相比与vue的路由集中式管理,能够很好的进行统一的路由操作,react的路由看起来更乱,想要进行像vue的全局路由管理不是那么得心应手。在我们的项目中,有很多页面是需要登陆权限验证的,最好的方式就是能够统一管理,而不是每个页面都要单独处理,下面是我的实现方法:

  首先我们建一个文件routerMap.js用来存储所有的路由信息,定义需要登陆拦截的页面(auth):

//routerMap.js

import Index from '../containers';
import Detail from '../containers/detail';
import Home from '../containers/home';
import List from '../containers/list';
import Topics from '../containers/topics';
import Parents from '../containers/passValue/parents';
import Children from '../containers/passValue/children';
import Request from '../containers/ajax';
import Like from '../containers/like';
import PopModule from '../containers/popModule/popModule';
import Reduxs from '../containers/redux/redux';
import Login from '../containers/login/login';
import Workers from '../containers/worker/worker';
import IndexedDB from '../containers/indexedDB/indexedDB'; export default [
{ path: "/", name: "App", component: Index },
{ path: "/home", name: "Home", component: Home },
{ path: "/topics", name: "Topics", component: Topics },
{ path: "/detail/:id", name: "Detail", component: Detail },
{ path: "/list", name: "List", component: List },
{ path: "/parents", name: "Parents", component: Parents },
{ path: "/children", name: "Children", component: Children },
{ path: "/ajax", name: "Request", component: Request, auth: true },
{ path: "/like", name: "Like", component: Like, auth: true },
{ path: "/popModule", name: "PopModule", component: PopModule, auth: true },
{ path: "/redux", name: "Reduxs", component: Reduxs, auth: true },
{ path: "/login", name: "Login", component: Login },
{ path: "/worker", name: "Worker", component: Workers },
{ path: "/indexedDB", name: "indexedDB", component: IndexedDB }
]

  然后在App.js里面引入:

 //App.js
import React from 'react';
import {
BrowserRouter as Router,
Route,
Switch,
Redirect
} from 'react-router-dom';
import { connect } from 'react-redux'
import Routers from './router/routerMap' // 公共头部组件
import Header from './common/header'
// 404页面
import NotFound from './containers/notFound' class App extends React.Component {
constructor(props) {
super(props)
}
render() {
let token = this.props.token
return (
<Router>
<div>
<Header />
<Switch>
{Routers.map((item, index) => {
return <Route key={index} path={item.path} exact render={props =>
(!item.auth ? (<item.component {...props} />) : (token ? <item.component {...props} /> : <Redirect to={{
pathname: '/login',
state: { from: props.location }
}} />)
)} />
})}
// 所有错误路由跳转页面
<Route component={NotFound} />
</Switch>
</div>
</Router>
)
}
} // redux拿到token并挂载到App的props上面
const mapStateToProps = (state, ownProps) => {
return { token: state.token }
} export default connect(mapStateToProps)(App)

  以上代码最重要的点是Route组建里面用render属性替换component来渲染页面,根据routerMap.js中的每一条路由信息中的auth(自定义)字段来区分是否需要进行登陆拦截,再根据redux里面的token字段来判断是不是登陆状态,然后进行相关的操作。如果已经拦截了就把当前的路由通过Redirect的state来传递到登陆页面,在登陆页面打印this.props来看控制台的输出:

  以上红框内的信息即为重定向之前的页面信息,如果登陆成功之后回跳from.pathname即可:

// 登陆成功方法 login.jsx
setToken() {
let token = this.state.user + this.state.pwd
if (!token) return
let RedirectUrl = this.props.location.state ? this.props.location.state.from.pathname : '/'
// 修改redux中的token值
this.props.changeActive(token)
// 登陆成功之后的跳转
this.props.history.push(RedirectUrl)
}

  react全局的登陆拦截方法到此就完成了

react-router-dom实现全局路由登陆拦截的更多相关文章

  1. react router @4 和 vue路由 详解(全)

    react router @4 和 vue路由 本文大纲: 1.vue路由基础和使用 2.react-router @4用法 3.什么是包容性路由?什么是排他性路由? 4.react路由有两个重要的属 ...

  2. React Router 4.0 实现路由守卫

    在使用 Vue 或者 Angular 的时候,框架提供了路由守卫功能,用来在进入某个路有前进行一些校验工作,如果校验失败,就跳转到 404 或者登陆页面,比如 Vue 中的 beforeEnter 函 ...

  3. react router @4 和 vue路由 详解(七)react路由守卫

    完整版:https://www.cnblogs.com/yangyangxxb/p/10066650.html 12.react路由守卫? a.在之前的版本中,React Router 也提供了类似的 ...

  4. react router @4 和 vue路由 详解(八)vue路由守卫

    完整版:https://www.cnblogs.com/yangyangxxb/p/10066650.html 13.vue路由守卫 a.beforeEach 全局守卫 (每个路由调用前都会触发,根据 ...

  5. React Router 4.0 ---- 嵌套路由和动态路由

    嵌套路由,从广义上来说,分为两种情况:一种是每个路由到的组件都有共有的内容,这时把共有的内容抽离成一个组件,变化的内容也是一个组件,两种组件组合嵌套,形成一个新的组件.另一种是子路由,路由到的组件内部 ...

  6. react router @4 和 vue路由 详解(六)vue怎么通过路由传参?

    完整版:https://www.cnblogs.com/yangyangxxb/p/10066650.html 8.vue怎么通过路由传参? a.通配符传参数 //在定义路由的时候 { path: ' ...

  7. react router @4 和 vue路由 详解(五)react怎么通过路由传参

    完整版:https://www.cnblogs.com/yangyangxxb/p/10066650.html 7.react怎么通过路由传参? a.通配符传参(刷新页面数据不丢失) //在定义路由的 ...

  8. react router @4 和 vue路由 详解(四)vue如何在路由里面定义一个子路由

    完整版:https://www.cnblogs.com/yangyangxxb/p/10066650.html 6.vue如何在路由里面定义一个子路由? 给父路由加一个 children:[] 参考我 ...

  9. react router @4 和 vue路由 详解(三)react如何在路由里面定义一个子路由

    完整版:https://www.cnblogs.com/yangyangxxb/p/10066650.html 5.react如何在路由里面定义一个子路由?   a.引入在需要子路由的页面引入Rout ...

随机推荐

  1. Ubuntu 中QT 用sogou拼音 安装

    1.下载搜狗输入法的安装包 下载地址为:http://pinyin.sogou.com/linux/ ,如下图,要选择与自己系统位数一致的安装包,我的系统是64位,所以我下载64位的安装包 2.按键C ...

  2. git下的团队合作模型及git基础知识汇集

    https://www.atlassian.com/git/tutorials/syncing/git-fetch Syncing svn使用单个中央库来作为开发者之间沟通的桥梁,而协同合作是通过在开 ...

  3. Python学习---重点模块之xml

    xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单 数据准备 <?xml version="1.0"?> <data&g ...

  4. 如何退出virtualbox scale mode

    进入scale mode之后,可能会退不出来 HOST Key + C. 默认是 右Ctrl + C

  5. php中的foreach问题(1)

    前言 php4中引入了foreach结构,这是一种遍历数组的简单方式.相比传统的for循环,foreach能够更加便捷的获取键值对.在php5之前,foreach仅能用于数组:php5之后,利用for ...

  6. C#图解教程读书笔记(第2章 C#编程概述)

    这章主要是一个对于C#程序的概括解释 和C/C++不同,不是用include声明引用的头文件,而是通过using的方式,声明引用的命名空间. 命名和C/C++类似,并且也是区分大小写的,这件事情在VB ...

  7. nginx里配置跨域

    发布于 881天前  作者 wendal  1404 次浏览  复制  上一个帖子  下一个帖子  标签: nginx 跨域 if ($request_method = OPTIONS ) { add ...

  8. 【C++】随机重命名MP3文件

    新置MP3一件,竟然没有随机播放的功能.坑啊!身为程序媛一枚,自己动手吧~ 获取当前路径: char buf[1000]; GetCurrentDirectory(1000,buf); string ...

  9. sqlserver 2008 r2 直接下载地址,可用迅雷下载

    转自 http://www.cnblogs.com/chinafine/archive/2010/12/23/1915312.html sqlserver 2008 r2 直接下载地址,可用迅雷下载 ...

  10. BZOJ3238:[AHOI2013]差异(SAM)

    Description Input 一行,一个字符串S Output 一行,一个整数,表示所求值 Sample Input cacao Sample Output 54 HINT 2<=N< ...