【发布时间】:2019-03-18 11:10:56
【问题描述】:
我对 React 路由器有一个问题。 这是我的应用程序类。我检查我的令牌,如果它有效,我会重定向到仪表板页面。
class App extends Component {
constructor(props){
super(props);
this.checkAuth = this.checkAuth.bind(this);
}
checkAuth(){
if(localStorage.getItem(ACCESS_TOKEN)){
getCurrentAccount().then(response => {
this.props.onLoadCurrentAccount({ account: response, isAuthenticated: true });
this.props.history.push('/');
});
}
}
componentDidMount() {
this.checkAuth();
}
render() {
return (
<div>
<Switch>
<Route path='/login' component={Login}/>
<Route path='/signup' component={Signup} />
<PrivateRoute authenticated={this.props.account.isAuthenticated} path='/' component={CoreLayout} />
<Route component={NotFound}/>
</Switch>
</div>
);
}
}
如果您授权,我有不同的路线到其他组件。 总是当我尝试重新加载页面时,我会转到“/”页面。例如:我留在'/contacts',但当我重新加载页面时,我转到'/'。我该如何解决?
class CoreLayout extends Component {
render() {
return (
<div>
<Menu/>
<Switch>
<Route exact path='/' component={Dashboard}/>
<Route path='/contacts' component={Contacts}/>
<Route path='/assignment' component={Assignment}/>
<Route path='/tasks' component={Tasks}/>
</Switch>
</div>
);
}
}
谢谢
【问题讨论】:
-
如果
this.props.history.push('/')加载了App组件,您将明确强制它重定向到/。你想让它做什么?
标签: reactjs react-router