umi路由权限
时间: 2023-07-28 09:20:35 浏览: 239
在 umi 中实现路由权限可以通过以下几个步骤:
1. 在路由配置文件中定义路由时,可以添加一个 `authority` 属性来标识该路由需要的权限。比如:
```
routes: [
{
path: '/',
component: '@/pages/index',
authority: ['admin', 'user']
},
...
]
```
2. 在应用程序中,可以通过一些方式获取当前用户的权限列表。比如从后端接口获取、从浏览器缓存中获取等等。
3. 在应用程序中,可以编写一个 `Authorized` 组件来控制路由是否可以渲染。该组件可以接收一个权限列表作为参数,然后根据当前用户的权限列表和路由配置中的权限要求,来判断当前路由是否可以渲染。比如:
```
import React from 'react';
import { Redirect, Route } from 'umi';
const Authorized = ({ children, authority }) => {
// 获取当前用户的权限列表
const currentUserAuthority = ['admin', 'user'];
// 判断当前路由是否可以渲染
if (authority.some(item => currentUserAuthority.includes(item))) {
return children;
} else {
return <Redirect to="/403" />;
}
};
export default Authorized;
```
4. 在路由配置文件中使用 `Authorized` 组件来包装需要进行权限控制的路由即可。比如:
```
routes: [
{
path: '/',
component: '@/layouts/index',
routes: [
{
path: '/',
component: '@/pages/index',
authority: ['admin', 'user'],
wrapper: Authorized
},
...
]
},
...
]
```
这样,只有当当前用户的权限列表中包含该路由需要的权限时,该路由才会被渲染出来。否则,用户将被重定向到 403 页面。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20250102104920.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)