wrappers是umi中用于路由鉴权的

路由中设置

export default {
  routes: [
    { path: '/user', component: 'user',
      wrappers: [
        '@/wrappers/auth',
      ],
    },
    { path: '/login', component: 'login' },
  ]
}

然后

然后在 src/wrappers/auth 中,

import { Redirect } from 'umi';
import { LocalStorage } from '@/utils/storage';
export default (props) => {
  const isLogin = LocalStorage.get('token');
  console.log(props.location);
  if (isLogin) {
    return <div>{props.children}</div>;
  } else {
    return (
      <Redirect to={{ pathname: '/login', state: { from: props.location } }} />
    );
  }
};

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐