我是React的新手,希望有人能夠闡明爲什麼會發生這種情況以及如何進行調試。React路由器無法獲取/取決於onEnter功能
我有以下途徑定義:
export default (withHistory, onUpdate) => {
const history = withHistory?
(Modernizr.history ?
new BrowserHistory
: new HashHistory)
: null;
return (
<Router history={history}>
<Route path='/login' component={Login} />
<Route path='/' component={Home} onEnter={requireAuth} />
</Router>
);
};
requireAuth
應該檢查用戶登錄,如果不將其重定向到登錄頁面:
function requireAuth(nextState, transition) {
transition.to("/login");
}
如果我離開transtion.to
調用並瀏覽到根URL我只看到一條消息,在調試器中沒有錯誤消息說「無法獲取/」。在該行上放置斷點似乎沒有任何作用。
特別奇怪的是,如果我用debugger;
語句替換transition.to(...)
,那麼調用該方法並且路由工作得很好。
我必須對某件事產生誤解,對於這是什麼想法?
編輯:我應該補充說,導航到host:port/login
工作正常,所以我知道login
路線的作品。
什麼版本的react-router? 1.x的? – walkerrandophsmith
我使用的是react-router版本1.0.0-beta3。我確實嘗試升級我使用的版本,但我最終以更簡單的方式解決了這個問題。 – shieldstroy