在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML5  HTML/ 在dva.js中如何攔截路由(做登錄認證),沒有類似vue中的beforeEac

在dva.js中如何攔截路由(做登錄認證),沒有類似vue中的beforeEach鉤子函數

現在公司的后臺系統(tǒng)使用螞蟻金服的dva.js,基于react+redux的封裝,使用的是dva-cli,現在要做登錄認證。
react-router-dom中好像沒有類似vue中的beforeEach鉤子函數,可以攔截路由。

import React from 'react';
import { Router, Route, Switch, Redirect } from 'dva/router';
import routerList from './common/routerList';
import Error404 from './routes/Error/Error404';

export default function RouterConfig({ history, app }) {
  const token = app._store.getState().user.token;
  console.log('router get token: ', token);
  const Routers = routerList.map((item, index) => {
    return <Route key={index} path={item.path} exact render={(props) => {
      if (item.noAuth) { // 如果是不用登錄就可訪問的頁面,直接返回
        return <item.component {...props} />;
      } else {
        if (token) {
          return <item.component {...props} />;
        } else {
          return <Redirect to={{
            pathname: '/login',
            state: {from: props.location} }} />
        }
      }
    }} />
  });
  return (
    <Router history={history}>
      <Switch>
        {Routers}
        <Route path="/" exact render={() => (
          <Redirect to="/article/mylist" />
        )} />
        <Route component={Error404} />
      </Switch>
    </Router>
  );
}

這是我做的路由攔截,但是只有在第一次進入頁面,或者頁面刷新后才會有效果,應該怎么改呢
然后登錄的時候,使用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
});
let RedirectUrl = this.props.location.state ? this.props.location.state.from.pathname : '/';
console.log(RedirectUrl);
this.props.history.replace(RedirectUrl);
回答
編輯回答
怣人

react-router4+在官方教程中給出了一個登錄認證的范例

類似于vue的鉤子函數是沒有的,因為沒有靜態(tài)路由配置表的概念。主要思想還是通過認證組件包裹你的那些涉及到認證信息的頁面組件,在認證組件中實現登錄重定向/被包裹組件渲染的邏輯。

2018年7月6日 21:17
編輯回答
空白格

不要用vue的思維去做react。
react是數據驅動的。一切皆數據。
dva對于路由的監(jiān)聽,可以放到modelsubscriptions中。

給你一個我們項目的initial project。

2017年12月2日 10:55