我试图实现一个条件重定向。
所以在一个函数中我使用了这个:
if(request.responseText==="true"){
window.location.href = '/home';
}
...
但是当我使用 react 时.js我正在尝试从react-router-dom
实现Redirect
。
所以我尝试了这段代码:
if(request.responseText==="true"){
<Router>
<Redirect to={"/home"}/>
</Router>
}
...
但是现在它甚至没有编译,它给了我错误:
期望赋值或函数调用,而是看到一个表达式 no-un-used-expression
我认为你不需要用Router
包裹它,你可以拥有这个,
if(request.responseText==="true"){
return <Redirect to="/home" />
}
你的组件应该被包装在withRouter中,参见 React Router - withRouter,那么你的组件现在在 props 中有历史记录。在您的示例中,像这样使用它:
if(request.responseText==="true"){
this.props.history.push('/home')
}
希望这有帮助!祝您编码愉快!:)
根据文档,它应该是
<Redirect to="/dashboard"/>
请注意,大括号已被移除。
https://github.com/ReactTraining/react-router/blob/master/packages/react-router/docs/api/Redirect.md