我有這些路線,我想包裹到checkAuth
方法來查看訪問者的會話狀態。爲了保持代碼的乾淨我隔了checkAuth
方法到一個單獨的文件,並將其導入與路線聲明文件:綁定一個REDX存儲到函數
import {checkAuth} from 'helpers/core'
export default (store) => {
return (
<Router history={browserHistory} onEnter={checkAuth.bind(store)}>
<Route path={AUTH_ROUTE} component={AuthLayout}>
<IndexRoute component={AuthView}/>
</Route>
<Route component={CoreLayout}>
<Route path={DASHBOARD_ROUTE} component={AuthView}/>
</Route>
</Router>
)
}
checkAuth
需要store
閱讀狀態,也派遣了一些行動,所以我不確定如何交上來。我嘗試使用綁定,因爲您可以在我的代碼中看到,但console.log(this)
在方法內返回undefined。
這裏的checkAuth
代碼:
export const checkAuth = (desiredRoute, redirect) => {
console.log(this);// returns undefined
const state = this.getState();// Cannot read property 'getState' of undefined
const isAuthenticated = state.auth.loggedIn;
....
};
是什麼阻止你在'checkAuth'模塊中要求'store'? – Oleg
'onEnter'不應該調用函數,所以我不能將括號放在那裏,只需將store作爲參數。 –