博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
react privateRoute
阅读量:6821 次
发布时间:2019-06-26

本文共 3696 字,大约阅读时间需要 12 分钟。

import React from 'react';import PropTypes from 'prop-types';import {Route,Redirect,withRouter} from 'react-router-dom';import LoginUser from 'service/LoginUser';//私有路由,只有登录的用户才能访问class PrivateRoute extends React.Component{    constructor(props) {        super(props);        this..state = {            isAuth : _loginUser.hasLogin();        }    }    componentWillMount(){        if(!isAuth){          const {history} = this.props;          setTimeout(() => {            history.replace("/login");          }, 1000)        }    }    render(){        const { component: Component,path="/",exact=false,strict=false} = this.props;        return this.state.isAuth ?  (            
(
)} /> ) :
; }}PrivateRoute.propTypes ={ path:PropTypes.string.isRequired, exact:PropTypes.bool, strict:PropTypes.bool, component:PropTypes.func.isRequired}export default withRouter(PrivateRoute);

 使用redux:

import { Component } from 'react'import { connect } from 'react-redux'import { get } from 'lodash'import PropTypes from 'prop-types'import toastr from 'toastr'import { Route } from 'react-router-dom'import { Redirect } from 'react-router'import { LoadingIndicator } from 'components'export class PrivateRoute extends Component {  constructor (props) {    super(props)    this.state = {      isLoading: true, // 是否於權限檢核中      isAuthed: false  // 是否通過權限檢核    }  }  static propTypes = {    component: PropTypes.any.isRequired,    funcCode: PropTypes.string.isRequired  }  checkAuth = async () => {    let isAuthed = false    const { isLogin, funcCode } = this.props    if (isLogin) {            this.setState(state => ({ ...state, isLoading: true }))      isAuthed = await api.checkAuthWithServer(funcCode)    }    if (!isAuthed) {           toastr.warning('系統未登录,请先登录')    }    // 更新狀態 1.檢核結束 2.檢核結果    this.setState(state => ({ ...state, isAuthed: isAuthed, isLoading: false }))  }  componentWillMount = async () => {    await this.checkAuth()  }  componentWillReceiveProps = async (nextProps) => {    if (nextProps.location.pathname !== this.props.location.pathname) {      await this.checkAuth()    }  }    render () {    const { component: Component, ...rest } = this.props    const { isLoading, isAuthed } = this.state    return (      isLoading === true        ? 
:
( isAuthed ?
:
)} /> ) }}const mapStateToProps = state => ({ // 登入系統後會於 redux 中註記登入狀態 isLogin: get(state, 'auth.isLogin')})const mapDispatchToProps = dispatch => ({})export default connect(mapStateToProps, mapDispatchToProps)(PrivateRoute)

 update privateRoute:

import React from 'react';import {toastr} from "react-redux-toastr";import {Route, withRouter} from 'react-router-dom';import LoginUser from 'service/login-service/LoginUser';import Unauthorized from "page/error/Unauthorized";const _loginUser = new LoginUser();//私有路由,只有登录的用户才能访问class PrivateRoute extends React.Component{    constructor(props) {        super(props);        this.state = {            isAuth : _loginUser.hasLogin()    }    }    componentWillMount(){        if(!this.state.isAuth){            toastr.error('login timeOut, return to the login page after 3s');            const {history} = this.props;            setTimeout(() => {                history.replace("/login");            }, 3000)        }    }    render(){        const { component: Component, path="/", exact=false, strict=false} = this.props;        return this.state.isAuth ?  (            
(
)} />) :
; }}export default withRouter(PrivateRoute);

 

转载于:https://www.cnblogs.com/Nyan-Workflow-FC/p/8794706.html

你可能感兴趣的文章
Apache Rewrite
查看>>
UML学习笔记(7)——时序图
查看>>
python爬虫基础
查看>>
Java 单例模式 学习
查看>>
Struts1.x系列教程(19):LookupDispatchAction类处理一个form多个submit
查看>>
ubuntu LTSP 无盘多终端ubuntu系统
查看>>
phpstorm支持CodeIgniter自动补全
查看>>
linux磁盘批量分区格式化和挂载脚本
查看>>
第一次尝试OSCHINA博客平台
查看>>
常用html、CSS、javascript前端命名规范
查看>>
EasyMock 用法
查看>>
postgresql事务处理与并发控制
查看>>
使用Apache的ab工具对比Nginx与Apache静态页面处理能力
查看>>
linux基本命令之用户篇
查看>>
C语言基于GTK+Libvlc实现的简易视频播放器(二)
查看>>
android自定义view无法预览
查看>>
时间格式2016-12-12 12:32:12.0带毫秒转换正常yyyy-MM-dd HH:mm:ss
查看>>
你百分百不知道的四大PS技巧
查看>>
linux 查看文件内容
查看>>
oracle开机启动脚本
查看>>