2017-02-24 40 views
5

使用「反應路由器」:「^ 3.0.2」,如何重置頁面重載反應路由器

當我重新加載頁面,我需要它去的默認視圖,它是目前這樣做。但是,歷史上的實際路線仍然與我在那裏刷新的路線相同。因此,該組件在不應該出現時重新安裝。

路由歷史

export const browserHistory = useRouterHistory(useBeforeUnload(createHistory))() 

路線

<Router history={browserHistory}> 
    <Route path='/' name='Auth' component={Auth}> 
     <IndexRoute 
     component={Dashboard} 
     onEnter={(nextState, replace) => replace('/login')} /> 
     <Route path='dashboard' name='Dashboard' component={Dashboard} /> 
     <Route path='resources' name='Resources' component={Resources} /> 
     <Route path='users' name='Users' component={UsersContainer} /> 
     <Route path='user/:params' name='User' component={UserContainer} /> 
    </Route> 
    <Route path='/' name='NoAuth' component={NoAuth}> 
     <Route path='login' name='Login Page' component={Login} /> 
    </Route> 
    </Router> 

這是我如何檢查,看看用戶是否仍然具有有效的會話令牌,以及如何我重新路由到儀表板。不知道我是否以最好的方式做到這一點。

const _checkAuth =() => { 
    if (profile) { 
    const res = JSON.parse(profile) 
    store.dispatch({ type: types.LOGIN_IDENTITY_SUCCESS, res }) 
    console.log(browserHistory.getCurrentLocation().pathname) 
    router.replace('/dashboard') 
    } 
} 

_checkAuth() 

回答

3

,如果你想推到與反應路由器可以使用.push方法推另一路由到堆棧中一個新的路線,這不會刪除在歷史上第途徑,如果這就是你正在尋找,但它會正確地推新航線投入反應路由器

const _checkAuth =() => { 
    if (profile) { 
    const res = JSON.parse(profile) 
    store.dispatch({ type: types.LOGIN_IDENTITY_SUCCESS, res }) 
    console.log(browserHistory.getCurrentLocation().pathname) 
    browserHistory.push('/dashboard'); 
    } 
} 

_checkAuth() 

只需更換router.replace('/dashboard')browserHistory.push('/dashboard')