react 路由拦截通过tocken进行简单拦截

//router.js

import React from 'react'

import { Route, Switch } from 'react-router-dom';

import Page1 from './pages/page1';

import Page2 from './pages/page2';

import Page3 from './pages/page3';

import Page4 from './pages/page4';

import InnerPage from './pages/InnerPage';

function isAuth(){

var tocken = localStorage.getItem('tocken')

if(tocken) return true

return false

}

export default function Router() {

return (

<Switch>

<Route path="/Page1" component={Page1}></Route>

<Route path="/Page2" render={()=>

{return isAuth()? <Page2></Page2>:<Page3></Page3>}

}></Route>

<Route path="/Page3" component={Page3}></Route>

<Route path="/Page4" component={Page4}></Route>

{/* <Route path="/InnerPage/:id" component={InnerPage}></Route> */}

<Route path="/InnerPage" component={InnerPage}></Route>

{/* <Redirect from="/" to="/Page4" /> */}

</Switch>

)

}

//page3.js

import { useHistory } from "react-router-dom"

function Page3() {

const history = useHistory()

const login=()=>{

localStorage.setItem('tocken','wwww')

history.push(`/Page1/`)

}

return (

<div>

<button onClick={login}>login</button>

</div>

);

}

export default Page3;

//app.js

import './App.css';

// import Clock2 from './components01/b.js'

// import Name from './components02/a';

//import Clock3 from './components01/c.js'

// import Name from './components02/a'

// import Clock6 from './components01/e.js'

// import HCom from './components01/h.js'

// import SwiperCom from './components01/swiper'

//import Bcom from './components02/b'

// import Ccom from './components02/c'

// import Dcom from './components02/d'

// import Jcom from './components02/j'

// import Fcom from './components02/f'

// import UseReducer from './components02/UseReducer'



 

import {Link,HashRouter,NavLink } from 'react-router-dom';

import Router from './router';

function App() {


 

return (

<HashRouter>

<div className="App" >

<ul className='ul-tab'>

<li><Link to='/Page1'>Page1</Link></li>

<li><Link to='/Page2'>Page2</Link></li>

<li><Link to='/Page3'>Page3</Link></li>

<li><NavLink to='/Page4'>Page4</NavLink></li>

</ul>

</div>

<Router></Router>

</HashRouter>

);

}

export default App;

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值