vue3中在router/index.js中

import { createRouter, createWebHistory } from 'vue-router';
import store from 'store' const userRule = {
path: 'user',
component: () => import('@/views/user/index.vue'),
children: [
{
path: 'list',
name: 'userList',
meta: { title: '用户列表', parents: '用户管理', keepAlive: true },
component: () => import('@/views/user/userList.vue'),
},
{
path: 'list',
name: 'userList',
meta: { title: '用户列表', parents: '用户管理', keepAlive: true },
component: () => import('@/views/user/userList.vue'),
}
]
}
const operationRule = {
path: 'opreation',
component: () => import('views/opreation/index.vue'),
}
const reviewRule = {
path: 'review',
component: () => import('views/review/index.vue'),
}
const addissuanceRule = {
path: 'addissuance',
component: () => import('views/addissuance/index.vue'),
}
const financeRule = {
path: 'finance',
component: () => import('views/finance/index'),
}
const statisticsRule = {
path: 'statistics',
component: () => import('views/statistics/index'),
}
const accountRule = {
path: 'account',
component: () => import('views/account/index'),
};
//映射相应的 权限
const ruleMapping = { //匹配路由名字获得对应组件
'user': userRule,
'review': reviewRule,
'operation': operationRule,
'addissuance': addissuanceRule,
'finance': financeRule,
'statistics': statisticsRule,
'account': accountRule,
}
//基础路由
const routes = [
{
path: "/",
name:'home',
meta:{title:'首页'},
component: () => import('views/Home.vue'),
}, {
path: '/login',
component: () => import('views/Login.vue')
},
{
path: "/:catchAll(.*)", //思路2.界面的控制: 用户手动输入任何不匹配的路由地址,就转到notFound页
component: () => import('views/404.vue')
}
] const router = createRouter({
history: createWebHistory(), //createWebHistory or createWebHashHistory
routes: routes
})
//动态添加路由
export function initDynamicRoutes() {
const currentRoutes = router.options.routes;
//在状态机中获取所有的菜单
const menuList = store.state.user.menuList;
//在状态机里获取登录人的权限
const rights = store.state.user.userInfo.rights; menuList.forEach(item => {
//循环所有的的菜单 通过 authority属性里的权限集合判断是否包含当前权限
if(item.authority.includes(rights)){
const temp = ruleMapping[item.name]; //↑↑匹配路由名字获取对应组件
// currentRoutes[0].children.push(temp);
//在vue2中可以通过router.addRoutes来添加一个数组,但是vue-router 4.0后没有addRoutes知道手动添加,此时可以通过router.push('/user/userlit')来进行跳转,
router.addRoute('home',temp)
}
})
}
let routerFlag = true;
router.beforeEach((to, from, next) => {
document.title = to.meta.title
let userInfo = store.getters['user/users'];
if (!userInfo && to.path != '/login') {
next('/login')
} else {
//通过router.addRoute动态添加的路由刷新页面后就没了,这里可以通过一个标识符routerFlag来判断,刷新后routerFlag为true,我们在手动添加一次就行了然后变成false
if(routerFlag){
initDynamicRoutes();
console.log(routerFlag);
next({path:to.path})
routerFlag = false;
}else{
next();
}
}
}) export default router;

vue2 中动态添加路由

//动态添加路由
export function initDynamicRoutes(){
console.log(router); const currentRoutes = router.options.routes;
const rightList = store.state.rightList; //获取vuex中路由权限数组 rightList.forEach( item => {
item.children.forEach(item =>{
const temp = ruleMapping[item.path]; //↑↑匹配路由名字获取对应组件
temp.meta = item.rights //将二级权限添加到 meta对象中. 思路4:请求&响应的控制就是通过匹配meta中的权限来操作的 currentRoutes[2].children.push(temp); //添加到 /home的childern中作为子路由
})
})
router.addRoutes(currentRoutes);
}

vue 动态路由添加的问题的更多相关文章

  1. Vue 动态路由的实现以及 Springsecurity 按钮级别的权限控制

    思路: 动态路由实现:在导航守卫中判断用户是否有用户信息,通过调用接口,拿到后台根据用户角色生成的菜单树,格式化菜单树结构信息并递归生成层级路由表并使用Vuex保存,通过 router.addRout ...

  2. vue动态路由

    我们经常需要把某种模式匹配到的所有路由,全都映射到同个组件.例如,我们有一个 User 组件,对于所有 ID 各不相同的用户,都要使用这个组件来渲染.能够提供参数的路由即为动态路由第一步:定义组件 c ...

  3. vue——动态路由以及地址传参

    动态路由: 当我们很多个页面或者组件都要被很多次重复利用的时候,我们的路由都指向同一个组件,这时候从不同组件进入一个”共用”的组件,并且还要传参数,渲染不同的数据 这就要用到动态路由跟路由传参了! 1 ...

  4. 18 vue 动态路由传参

    params形式 http://192.168.1.100:8080/#/infoDetailed/231 //定义路由{ path: "/infoDetailed/:newsId" ...

  5. Vue 动态路由传值

    一.动态路由传值 1.配置动态路由: const routes = [ //动态路由路径参数以:开头 { path: '/Content/:aid', component:Content}, ] 2. ...

  6. vue 动态路由 Get传值

    main.js //2.配置路由 注意:名字 const routes = [ { path: '/home', component: Home }, { path: '/news', compone ...

  7. vue动态路由配置,vue路由传参

    动态路由: 当我们很多个页面或者组件都要被很多次重复利用的时候,我们的路由都指向同一个组件,这时候从不同组件进入一个"共用"的组件,并且还要传参数,渲染不同的数据 这就要用到动态路 ...

  8. Vue动态路由 Get传值

    <template> <!-- 所有的内容要被根节点包含起来 --> <div id="home"> 我是首页组件 <ul> < ...

  9. vue动态路由传值以及get传值及编程式导航

    1.动态路由传值 1.配置路由处 { path: '/content/:id', component: Content }, // 动态路由 2.对应页面传值 <router-link :to= ...

  10. vue 动态路由按需加载的三种方式

    在Vue项目中,一般使用vue-cli构建项目后,我们会在Router文件夹下面的index.js里面引入相关的路由组件,如: import Hello from '@/components/Hell ...

随机推荐

  1. 使用async实现多个请求并发

    // 写法一 let [cache, cache2] = await Promise.all([cachePromise, cachePromise2]); // 写法二 let Promise= c ...

  2. k8s 1.20.5(补充)

    1.根据前面1.15.0补充 2.初始化操作 selinux swap firewall关闭防火墙 swapoff -a 禁用交换空间 vim /etc/sysctl.d/k8s.conf net.b ...

  3. 使用Libusb测试USB device

    一. 先准备好测试工具 -- Libusb: 在Linux中使用的话: 首先从 http://www.libusb.org/官网中下载libusb 然后解压之后./configure --> m ...

  4. 第二周集训队比赛wp

    Web1 直接截包改get为Host: Web2: 不一样的猪圈 普通的猪圈变种 NormalPwn 用IDA查看发现main下面有一个危险函数点进去发现/bin/sh 进main空格发现找cmman ...

  5. mybatis中xml新增一条数据获取自增id

    在insert的标签里加两个属性:useGeneratedKeys="true"         keyProperty="patentId"   ,这个key ...

  6. nodejs查询

    const tagModel = require("../mongodb"); 1 const mongoose = require("mongoose"); ...

  7. pytorch的三种量化方式详解

    pytorch的三种量化方式详解 这篇博客详细介绍了pytorch官方教程提到的三种量化方式的原理,详细解释了三种量化方式的区别: 1. 静态量化 :torch.quantize_per_tensor ...

  8. k8s如何配置secret保存harbor仓库账号密码、pod中怎么使用harbor仓库镜像

    转载: https://blog.csdn.net/MssGuo/article/details/127312239

  9. keycloak 找出特定客户端权限的user 配置OTP

    背景:项目组中有用到keycloak给两个应用进行登录认证使用.其中有一个应用放在公网,安全部门同事说 不能直接账号密码登录,容易破解,需要进行二次验证. 刚好查到keycloak支持OTP(one ...

  10. SAP S/4HANA Cloud的功能亮点以及大中型企业为何更倾向选择它

    SAP-System Applications and Products,是一家来自德国的大型跨国软件公司,成立于1972年.作为全球企业管理和协同化商务解决方案供应商,世界第三大独立软件供应商和全球 ...