您将要使用该
Redirect组件。有几种不同的方法可以解决此问题。我喜欢的一个是,有一个PrivateRoute组件,该组件接受一个
authed道具,然后根据该道具进行渲染。
function PrivateRoute ({component: Component, authed, ...rest}) { return ( <Route {...rest} render={(props) => authed === true ? <Component {...props} /> : <Redirect to={{pathname: '/login', state: {from: props.location}}} />} /> )}现在你
Route的可以看起来像这样
<Route path='/' exact component={Home} /><Route path='/login' component={Login} /><Route path='/register' component={Register} /><PrivateRoute authed={this.state.authed} path='/dashboard' component={Dashboard} />如果您仍然感到困惑,我写了这篇文章可能会有所帮助 -React Router
v4的受保护路由和身份验证



