在src文件夹下创建一个wrappers文件夹,并创建一个index.tsx
代码如下:
import React from 'react';
import { Redirect } from 'umi';
const AuthRouter = (props: any) => {
// 这个根据自己判断是否登录
const token = window.sessionStorage.getItem('user') ? true : false;
return token ? <div>{props.children}</div> : <Redirect to="/login" />;
};
export default AuthRouter;
2.路由配置
const router = [
{path:'/login',component:'@/pages/login'},
{path:"/home",component:'@/pages/home',wrappers: ['@/wrappers'],}
]
通过 umi 的 defineConfig 方法定义配置,在routes中定义配置的路由
import { defineConfig } from 'umi';
import list from './config';
export default defineConfig({
routes: list,
});