Javascript 如何通过history.push传入对象?
如何在/启动声音上播放声音,在/停止声音上停止声音 /start soundJavascript 如何通过history.push传入对象?,javascript,reactjs,react-router,Javascript,Reactjs,React Router,如何在/启动声音上播放声音,在/停止声音上停止声音 /start sound const history = useHistory(); const startSound = () => { const sound = new Audio("test.mp3"); sound.play(); history.push("/stop-sound", [sound]); } const stopSound = () => { sound.pause(); soun
const history = useHistory();
const startSound = () => {
const sound = new Audio("test.mp3");
sound.play();
history.push("/stop-sound", [sound]);
}
const stopSound = () => {
sound.pause();
sound.currentTime = 0;
}
/stop sound
const history = useHistory();
const startSound = () => {
const sound = new Audio("test.mp3");
sound.play();
history.push("/stop-sound", [sound]);
}
const stopSound = () => {
sound.pause();
sound.currentTime = 0;
}
此代码将在浏览器中显示错误
DataCloneError:未能对“历史记录”执行“pushState”:无法克隆对象
此样式将遵循React路由器文档 我不知道如何使用声音对象和/停止声音
另一个例子 /pass text
const PassTextPage = () => {
const history = useHistory();
const passText= () => {
history.push({
pathname: "/view-text",
state: { name: "Hello" }
});
};
return <button onClick={passText}>pass</button>;
};
const ViewTextPage = () => {
const text = props.location.state.name; // 'props' is not defined.
const viewText = () => {
console.log(text);
};
return <button onClick={viewText}>view</button>;
};
constpasstextpage=()=>{
const history=useHistory();
常量passText=()=>{
历史推送({
路径名:“/查看文本”,
州:{name:“你好”}
});
};
回程通行证;
};
/查看文本
const PassTextPage = () => {
const history = useHistory();
const passText= () => {
history.push({
pathname: "/view-text",
state: { name: "Hello" }
});
};
return <button onClick={passText}>pass</button>;
};
const ViewTextPage = () => {
const text = props.location.state.name; // 'props' is not defined.
const viewText = () => {
console.log(text);
};
return <button onClick={viewText}>view</button>;
};
const ViewTextPage=()=>{
const text=props.location.state.name;//“props”未定义。
const viewText=()=>{
console.log(文本);
};
返回视图;
};
App.jsx
const App = () => (
<BrowserRouter>
<Route exact path="/">
<Home />
</Route>
<Route exact path="/pass-text">
<PassTextPage />
</Route>
<Route exact path="/view-text">
<ViewTextPage />
</Route>
</BrowserRouter>
);
const-App=()=>(
);
将属性传递给导航组件的方式
history.push({
pathname: '/stop-sound',
state: { name: 'Hello'}
});
let location = useLocation();
location.state.name
this.props.location.state.name
在导航组件中进行类似的访问
如果它的无状态组件
history.push({
pathname: '/stop-sound',
state: { name: 'Hello'}
});
let location = useLocation();
location.state.name
this.props.location.state.name
基于类的组件
history.push({
pathname: '/stop-sound',
state: { name: 'Hello'}
});
let location = useLocation();
location.state.name
this.props.location.state.name
将属性传递给导航组件的方式
history.push({
pathname: '/stop-sound',
state: { name: 'Hello'}
});
let location = useLocation();
location.state.name
this.props.location.state.name
在导航组件中进行类似的访问
如果它的无状态组件
history.push({
pathname: '/stop-sound',
state: { name: 'Hello'}
});
let location = useLocation();
location.state.name
this.props.location.state.name
基于类的组件
history.push({
pathname: '/stop-sound',
state: { name: 'Hello'}
});
let location = useLocation();
location.state.name
this.props.location.state.name
谢谢你的回答。在stop-sound.jsx中写入“props.location.state.name”时,显示错误“'prop'未定义”。你还缺什么吗?我在问题中添加了详细的代码,如果您愿意,请再次回答。很抱歉问了这么简单的问题。谢谢你的回答。在stop-sound.jsx中写入“props.location.state.name”时,显示错误“'prop'未定义”。你还缺什么吗?我在问题中添加了详细的代码,如果您愿意,请再次回答。对于这个基本问题,很抱歉。在const ViewTextPage=(props)中包含props我遇到错误“TypeError:无法读取未定义的属性'location'。发布ViewTextPage组件的父组件为两个组件添加了父组件。更改父组件类似于这样的内容:在const ViewTextPage=(props)中包含props我收到错误“TypeError:无法读取未定义的属性'location'。发布viewTextPage component的父组件为两个组件添加了父组件。更改父组件如下