Reactjs this.props.history.push未在react中工作
我试图在App.jsx中使用历史推送导航。 我使用了下面的代码Reactjs this.props.history.push未在react中工作,reactjs,react-router,Reactjs,React Router,我试图在App.jsx中使用历史推送导航。 我使用了下面的代码 componentDidMount() { if(!this.authService.isAuthenticated()) { this.props.history.push('/Login'); } } 但它给出了一个错误,因为历史是未定义的。 我尝试记录道具对象,得到以下输出 请帮助我在app.jsx内部导航。使用w
componentDidMount() {
if(!this.authService.isAuthenticated())
{
this.props.history.push('/Login');
}
}
但它给出了一个错误,因为历史是未定义的。
我尝试记录道具对象,得到以下输出
请帮助我在app.jsx内部导航。使用withRouter导出组件
import { withRouter } from 'react-router'
使用路由器导入
import { withRouter } from 'react-router'
像这样导出组件
export default withRouter(MyComponent)//your component name
导出此组件时,您必须使用withRouter。您能解释一下为什么您认为历史道具首先应该在那里吗?您是这样将历史道具传递给路由器的吗???有关withRouter的更多信息,请参阅此处->