vue3 + ts +pinia+element-plus+mock 项目---动态路由+用户权限路由篇
时间: 2023-12-10 20:04:06 浏览: 244
为了实现动态路由和用户权限路由,我们需要进行以下步骤:
1. 安装依赖
```shell
npm install vue-router@4 pinia element-plus mock -S
npm install @types/mock -D
```
2. 创建路由配置文件
在src文件夹下创建router文件夹,并在其中创建index.ts文件,用于配置路由。在该文件中,我们需要定义路由的各个页面组件,并根据需要配置路由的子路由和路由守卫。例如:
```typescript
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import Home from '@/views/home/indexName.vue'
import Login from '@/views/login/index.vue'
import NotFound from '@/views/404/index.vue'
const routes: Array<RouteRecordRaw> = [
{
path: '/',
name: 'Home',
component: Home,
meta: {
title: '首页',
requireAuth: true // 需要登录才能访问
}
},
{
path: '/login',
name: 'Login',
component: Login,
meta: {
title: '登录'
}
},
{
path: '/:pathMatch(.*)*',
name: 'NotFound',
component: NotFound,
meta: {
title: '404'
}
}
]
const router = createRouter({
history: createWebHistory(),
routes
})
// 路由守卫
router.beforeEach((to, from, next) => {
const token = localStorage.getItem('token')
if (to.meta.requireAuth && !token) {
next('/login')
} else {
next()
}
})
export default router
```
3. 在main.ts中挂载路由
在main.ts中,我们需要将路由配置文件挂载到Vue实例中,以便在应用中使用路由。例如:
```typescript
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
createApp(App).use(router).mount('#app')
```
4. 创建权限控制文件
在src文件夹下创建permission文件夹,并在其中创建index.ts文件,用于控制用户权限。在该文件中,我们需要定义用户的权限列表,并根据需要判断用户是否有权限访问某个路由。例如:
```typescript
import router from '@/router'
const whiteList = ['/login'] // 不需要登录即可访问的页面
router.beforeEach((to, from, next) => {
const token = localStorage.getItem('token')
if (token) {
if (to.path === '/login') {
next('/')
} else {
// 判断用户是否有权限访问该路由
const hasPermission = true // 根据实际情况判断用户是否有权限
if (hasPermission) {
next()
} else {
next('/404')
}
}
} else {
if (whiteList.indexOf(to.path) !== -1) {
next()
} else {
next('/login')
}
}
})
```
5. 在App.vue中挂载路由渲染入口
在App.vue中,我们需要将路由渲染入口挂载到模板中,以便在应用中渲染路由。例如:
```html
<template>
<div id="app">
<router-view />
</div>
</template>
```
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044736.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.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)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)