在 UmiJS 中,约定式路由的权限分配可以通过以下几种方式实现:

  1. 基于路由配置:在路由配置文件中,通过配置 'authority' 属性来指定该路由所需的权限。例如:
export default [
  {
    path: '/',
    component: './Home',
    authority: ['admin', 'user'],
  },
  {
    path: '/admin',
    component: './Admin',
    authority: ['admin'],
  },
];

上述代码中,'Home' 路由需要 'admin' 和 'user' 权限,而 'Admin' 路由只需要 'admin' 权限。

  1. 基于路由渲染:在渲染路由之前,可以通过自定义的方式进行权限校验,然后再决定是否渲染该路由。例如:
import { Redirect } from 'umi';

export function render(oldRender) {
  // 进行权限校验
  const hasPermission = checkPermission();

  if (hasPermission) {
    oldRender();
  } else {
    oldRender(<Redirect to='/login' />);
  }
}

上述代码中,通过 'checkPermission' 函数进行权限校验,如果有权限则继续渲染路由,否则重定向到登录页。

  1. 基于路由守卫:通过 UmiJS 提供的 'umi-plugin-antd' 插件,可以使用 'Authorized' 组件进行路由守卫。例如:
import { Authorized } from 'umi';

const AuthorizedRoute = Authorized.Route;

export default function(props) {
  return (
    <AuthorizedRoute
      {...props}
      authority={['admin']}
      redirectPath='/login'
    />
  );
}

上述代码中,通过 'Authorized.Route' 组件进行路由守卫,'authority' 属性指定该路由所需的权限,'redirectPath' 属性指定无权限时的重定向路径。

以上是一些常用的方式来实现约定式路由的权限分配,具体的实现方式可以根据项目需求进行选择和定制。

UmiJS 约定式路由权限分配:三种常用方法

原文地址: https://www.cveoy.top/t/topic/pgtr 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录