国产99久久精品_欧美日本韩国一区二区_激情小说综合网_欧美一级二级视频_午夜av电影_日本久久精品视频

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當前位置: 首頁 - 科技 - 知識百科 - 正文

React從react-router路由上做登陸驗證控制的方法

來源:懂視網 責編:小采 時間:2020-11-27 22:15:00
文檔

React從react-router路由上做登陸驗證控制的方法

React從react-router路由上做登陸驗證控制的方法:本文介紹了React從react-router路由上做登陸驗證控制的方法,分享給大家,具體如下: 驗證代碼 import React from 'react' import {connect} from 'react-redux'; function requireAuthentication(Component) {
推薦度:
導讀React從react-router路由上做登陸驗證控制的方法:本文介紹了React從react-router路由上做登陸驗證控制的方法,分享給大家,具體如下: 驗證代碼 import React from 'react' import {connect} from 'react-redux'; function requireAuthentication(Component) {

本文介紹了React從react-router路由上做登陸驗證控制的方法,分享給大家,具體如下:

驗證代碼

import React from 'react'
import {connect} from 'react-redux';

function requireAuthentication(Component) {
 // 組件有已登陸的模塊 直接返回 (防止從新渲染)
 if (Component.AuthenticatedComponent) {
 return Component.AuthenticatedComponent
 }

 // 創建驗證組件
 class AuthenticatedComponent extends React.Component {
 static contextTypes = {
 router: React.PropTypes.object.isRequired,
 }

 state = {
 login: true,
 }

 componentWillMount() {
 this.checkAuth();
 }

 componentWillReceiveProps(nextProps) {
 this.checkAuth();
 }

 checkAuth() {

 // 判斷登陸
 const token = this.props.token;
 const login = token ? token.login : null;


 // 未登陸重定向到登陸頁面
 if (!login) {
 let redirect = this.props.location.pathname + this.props.location.search;
 this.context.router.push('/login?message=401&redirect_uri=' + encodeURIComponent(redirect));
 return;
 }

 this.setState({login});
 }

 render() {
 if (this.state.login) {
 return <Component {...this.props}/>
 }
 return ''
 }
 }

 // 不使用 react-redux 的話直接返回
 // Component.AuthenticatedComponent = AuthenticatedComponent
 // return Component.AuthenticatedComponent


 function mapStateToProps(state) {
 return {
 token: state.token,
 };
 }

 function mapDispatchToProps(dispatch) {
 return {};
 }
 Component.AuthenticatedComponent = connect(mapStateToProps, mapDispatchToProps)(AuthenticatedComponent);
 return Component.AuthenticatedComponent
}

路由上使用

<Router history={browserHistory}>
 <Route path="/admin" component={requireAuthentication(AdminComponent)} />
</Router>

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

React從react-router路由上做登陸驗證控制的方法

React從react-router路由上做登陸驗證控制的方法:本文介紹了React從react-router路由上做登陸驗證控制的方法,分享給大家,具體如下: 驗證代碼 import React from 'react' import {connect} from 'react-redux'; function requireAuthentication(Component) {
推薦度:
  • 熱門焦點

最新推薦

猜你喜歡

熱門推薦

專題
Top
主站蜘蛛池模板: 欧美韩日在线 | 伊人久久中文字幕久久cm | 91麻豆精品国产91久久久 | 国产欧美在线观看不卡 | 欧美一区二区三区在线 | 亚洲精品免费在线观看 | 黄色免费网站视频 | 成人免费视频一区二区 | 青青草国产免费国产是公开 | 中国一级全黄的免费观看 | aa级毛片 | 国产精品一区二区午夜嘿嘿嘿小说 | 青青青久久久 | 91精品导航 | 国产精品久久二区三区色裕 | 国产最新网址 | 国产欧美久久久精品影院 | 亚洲精品日韩中文字幕久久久 | 国产综合精品久久久久成人影 | 人禽性动交异族另类 | 中文欧美日韩 | 国产精品激情综合久久 | 人与鲁牲交持级毛片 | 日韩欧美专区 | 99久久综合 | 在线视频一二三区 | 久久国产一级毛片一区二区 | 国产2区| 国产未成女年一区二区 | 亚洲一二三区在线观看 | 日日久| 久久亚洲国产成人精品性色 | 欧美精品免费在线 | 欧美亚洲天堂 | 日韩成人免费在线视频 | 精品在线免费视频 | 精品一区 二区三区免费毛片 | 国产精品人成人免费国产 | 亚洲一区中文字幕在线观看 | 国产在线高清视频 | 91大神在线精品视频一区 |