用途:看着用.....
功能:
1.没有token时,跳到登录页
2.有token时,追踪跳到对应的页面
核心: src/component/AuthRoute.js
import {Route,Redirect} from 'react-router-dom';
import { gettoken } from '@/utils';
export const AuthRoute=({children,...rest})=>{
//!!非空 判断本地是否有toekn
return <Route
{...rest}
render={(props)=>{
if(!!gettoken()){
//继续渲染的是component / children
return children
}
console.log(props);
return <Redirect to={
{pathname:"/login",form:{state:props.location.pathname}}}></Redirect>
}}
>
</Route>
}
src/App.js
import { AuthRoute } from './components/Anthroute';