/* * @Description: * @Author: pang bo * @Date: 2021-06-28 16:18:17 * @LastEditTime: 2021-06-28 17:20:07 * @LastEditors: Do not edit */ import React, { useEffect } from 'react'; import Login from '@src/views/Login' import App from '@src/views/App' import { Switch, Route, Redirect, HashRouter, match } from 'react-router-dom'; import Home from '@src/views/Home'; import { RouteComponentProps, StaticContext } from 'react-router'; const privateRouter = (params: RouteComponentProps<{}, StaticContext, unknown>, Template: any) => { const { location } = params; if (!localStorage.getItem('token')) { return <Redirect to='/login' /> } else { if (location.pathname === '/login') { return <Redirect to='/' /> } return <Template /> } } const Index = () => { return ( <HashRouter> <Switch> <Route path="/" exact render={(params) => privateRouter(params, App)}></Route> <Route path="/login" exact render={(params) => privateRouter(params, Login)}></Route> <Route path="/home" exact render={(params) => privateRouter(params, Home)}></Route> <Redirect to='/' /> </Switch> </HashRouter> ) } export default Index;
正文
react路由拦截
文章版权声明:除非注明,否则均为
譬如朝露_策温技术开发工作室博客原创文章,转载或复制请以超链接形式并注明出处。
发表评论
侧栏公告
寄语
譬如朝露博客是一个分享前端知识的网站,联系方式11523518。
热评文章
标签列表
热门文章
友情链接