好吧,我厌倦了尝试。
The onEnter
方法不起作用。知道为什么吗?
// Authentication "before" filter
function requireAuth(nextState, replace){
console.log("called"); // => Is not triggered at all
if (!isLoggedIn()) {
replace({
pathname: '/front'
})
}
}
// Render the app
render(
<Provider store={store}>
<Router history={history}>
<App>
<Switch>
<Route path="/front" component={Front} />
<Route path="/home" component={Home} onEnter={requireAuth} />
<Route exact path="/" component={Home} onEnter={requireAuth} />
<Route path="*" component={NoMatch} />
</Switch>
</App>
</Router>
</Provider>,
document.getElementById("lf-app")
Edit:
当我调用时该方法被执行onEnter={requireAuth()}
,但显然这不是目的,我也不会得到想要的参数。
onEnter
不再存在于react-router-4
。你应该使用<Route render={ ... } />
以获得您想要的功能。我相信Redirect https://reacttraining.com/react-router/web/api/Redirect示例有您的具体场景。我在下面修改了它以匹配您的。
<Route exact path="/home" render={() => (
isLoggedIn() ? (
<Redirect to="/front"/>
) : (
<Home />
)
)}/>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)