現(xiàn)在公司的后臺系統(tǒng)使用螞蟻金服的dva.js,基于react+redux的封裝,使用的是dva-cli,現(xiàn)在要做登錄認證。react-router-dom中好像沒有類似vue中的beforeEach鉤子函數(shù),可以攔截路由。importReactfrom'react';import{Router,Route,Switch,Redirect}from'dva/router';importrouterListfrom'./common/routerList';importError404from'./routes/Error/Error404';exportdefaultfunctionRouterConfig({history,app}){consttoken=app._store.getState().user.token;console.log('routergettoken:',token);constRouters=routerList.map((item,index)=>{return{if(item.noAuth){//如果是不用登錄就可訪問的頁面,直接返回return;}else{if(token){return;}else{return}}}}/>});return({Routers}()}/>);}這是我做的路由攔截,但是只有在第一次進入頁面,或者頁面刷新后才會有效果,應該怎么改呢然后登錄的時候,使用this.props.history.push()或者this.props.history.replace()也不能跳轉到首頁this.props.dispatch({type:'user/saveUserInfo',payload:{userId,nickName,headImageUrl,userName}});this.props.dispatch({type:'user/saveToken',payload:token});letRedirectUrl=this.props.location.state?this.props.location.state.from.pathname:'/';console.log(RedirectUrl);this.props.history.replace(RedirectUrl);
在dva.js中如何攔截路由(做登錄認證),沒有類似vue中的beforeEach鉤子函數(shù)
一只萌萌小番薯
2019-05-12 13:07:11