难受就摸头盖骨

vue-router 根据权限动态设置路由 theme.js" as it exceeds the max of "500KB".

需求: 根据不同角色的登录人,展示不同的功能模块. 前端路由在后台维护,动态注册路由.  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
业务流程:
  首先,登录成功,获取token
  其次,处理数据格式,主要是component的格式是,例如: import(`@view/user/${item.path}`)
  最后,通过this.addroutes()注册动态路由  <br>
关键技术
  addRoutes:
    解释:动态注册路由,router是在vue实例化的时候就已经注册挂载,所以,官方提供了一个动态的注册的api.
    使用方法1:this.$router.addRoues = ([{title: '人员管理', path: 'name}])
 
    使用方法2:
        const router = new VueRouter();
        router.addRoutes();
 
坑:
  1.component必须是函数,且不能使用字符拼接的方式生成组件路径.
    如:
        标准:component: () => import('@view/user.vue');
        错误:component: '@view/user.vue';
        错误:component: () => import('@view/${item.path}');
 
  2.不能存储在localStorege等浏览器缓存内,会修改component的数据格式
    如:存的时候是 component: () => import('@view/user.vue');
        取时变成:component: xxxxx
 
  3.需要在全局的"前置路由拦截"获取"角色模块",同时避免路由进入死循环.
 
 
前端路由对照表
reouterReference.js
 
const routerReference = {
    // 根据p路由path格式化component
    'workshop': () => import('@/views/workshop/index.vue'),
    'path': () => import('@/views/user/index.vue'),
}
expport default routerReference;
 
getRoleModuleList.js
//从服务器获取路由,格式化component
axios('module/list',{}, {authorToken: '123123123'})
.then(res) => {
    if (res.data.status === 200) {
        this.moduleLsit = res.data.res;
        this.moduleList.map(item => {
            item.component = reouterReference(route.path);// 创建component
            return item;      
        })
    };
}
 
注: 此处省略递归处理数据逻辑
 
router.beforeEach((to, from, next) => {
    addRouterArr = store.state.addRouterList;
    // 已登录
    if (Cookies.get('token')) { // 未登录且前往的页面不是登录页
        // 已经登录且前往的是登录页
        if (Cookies.get('user') && to.name === 'login') {
            Util.title();
            next({
                name: 'home_index'
            });
        } else {
            // 角色对应的模块存在的情况下
            if (addRouterArr && addRouterArr.length !== 0) {
                // 缓存打开的路由
                localStorage.setItem('activeRouteName', JSON.stringify({
                    name: to.name,
                    query: to.query,
                    params: to.params
                }));
                next();
            } else {
                // 获取角色对应的模块
                getRoleModuleList(to, next);
            };
            // next();
        }
        iView.LoadingBar.finish();
    } else if (!Cookies.get('token')) {
        if (to.name === 'login') {
            next();
        } else {
            next({
                name: 'login'
            });
        };
        // 未登录
        iView.LoadingBar.finish();
    }
 
});

  

posted @   longpanda_怪怪哉  阅读(6799)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
下定决心了,要把写不出代码就摸后脑勺的习惯给改了! 如果可以~单身待解救~~
点击右上角即可分享
微信分享提示