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

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

流程:

  首先,登录成功,获取token

  其次,通过在请求头携带当前登录人的token,调取module的接口

axios(‘module.list‘).then(res => {
  if (res.data.status === 200) {
    this.moduleList = res.data.res;
  }
})

  接着,处理数据格式,主要是component的格式是,例如: import(`@view/user/${item.path}`)

    

this.newAddRouter = this.moduleList.map(item => {
    return {
       code: item.code,
    icon: item.iconUrl,
    name: item.routerPath,    path: item.routerPath,    title: item.name,    id: item.id,    component: () => import(`@/views/tv/tv-nine/${item.path}`), // 正确的写法     // component: () => import(`${item.path}`), // 报错 theme.js" as it exceeds the max of "500KB".      parentId: item.parentId, 
    children: []    }; 
});

此处省略递归处理

  

  最后,通过this.addroutes()注册动态路由  

this.$router.addroutes(newAddRouter);注: 由于我们的需求是在登录成功后才根据登录人的角色动态生成模块,而router是在vue实例化的时候就已经注册挂载,所以,官方提供了一个动态的注册的api,它就是addroutes.

  

  

原文地址:https://www.cnblogs.com/web-zqk/p/11729550.html

时间: 2024-08-30 15:30:49

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

vue router的 index.js设置

import Vue from 'vue' import VueRouter from 'vue-router' import login from '../pages/login/index' import Main from '../pages' import Register from '../pages/register/register' import noFound from '../components/404' import noPerm from '../components/

Vue.js路由管理器 Vue Router

起步 HTML <script src="https://unpkg.com/vue/dist/vue.js"></script> <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script> <div id="app"> <h1>Hello App!</h1> <p>

Vue系列:Vue Router 路由梳理

Vue Router 是 Vue.js 官方的路由管理器.它和 Vue.js 的核心深度集成,让构建单页面应用变得易如反掌.包含的功能有: 嵌套的路由/视图表 模块化的.基于组件的路由配置 路由参数.查询.通配符 基于 Vue.js 过渡系统的视图过渡效果 细粒度的导航控制 带有自动激活的 CSS class 的链接 HTML5 历史模式或 hash 模式,在 IE9 中自动降级 自定义的滚动条行为 1.动态路由 动态路由,可以将某种模式匹配到的所有路由,并全都映射到同个组件. (通俗点,比如根

vue router 路由的学习

新建vue项目的时候 你会发现有个文件夹叫router 这个文件夹下你可以设置一个index.js但是需要引入的两个包 import Vue from 'vue' import Router from 'vue-router' Vue.use(Router) 注册路由组件 export default new Router 默认加载这个路由 routes: [] 内部可以写你要配置的路由路径 path: '/' //配置地址 多数用来跳转的地址 nam: 'bac'// 一般用来路由跳转this

vue 动态获取路由在对组件进行处理是报错,导致无法进入页面

vue 动态获取路由在对组件进行处理是报错,导致无法进入页面function filterAsyncRouter(asyncRouterMap) { //遍历后台传来的路由字符串,转换为组件对象const accessedRouters = asyncRouterMap.filter(route => { if (route.component) { if (route.component === 'Layout') {//Layout组件特殊处理 route.component = Layou

Vue Router 路由实现原理实现原理

一.概念 通过改变 URL,在不重新请求页面的情况下,更新页面视图. 二.实现方式 更新视图但不重新请求页面,是前端路由原理的核心之一,目前在浏览器环境中这一功能的实现主要有2种方式: 1.Hash --- 利用 URL 中的hash("#"); 2.利用 History interface 在HTML5中新增的方法. Vue 中,它是通过 mode 这一参数控制路由的实现模式: const router=new VueRouter({ mode:'history', routes:[

&lt;keep-alive&gt;控制Vue Router路由

只给部分组件加上<keep-alive>啊,在app.vue里这样 <!-- 这里是需要keepalive的 --> <keep-alive> <router-view v-if="$route.meta.keepAlive"></router-view> </keep-alive> <!-- 这里不会被keepalive --> <router-view v-if="!$route.m

【转】vue中动态设置meta标签和title标签

因为和原生的交互是需要h5这边来提供meta标签的来是来判断要不要显示分享按钮,所有就需要手动设置meta标签,标题和内容 //router内的设置 { path: '/teachers', name: 'TDetail', component: TDetail, meta: { title:"教师详情", content: 'disable' } }, { path: '/article', name: 'Article', component: Article, meta: { t

【转】Vue-详解设置路由导航的两种方法: &lt;router-link :to=&quot;...&quot;&gt; 和router.push(...)

一.<router-link :to="..."> to里的值可以是一个字符串路径,或者一个描述地址的对象.例如: // 字符串 <router-link to="apple"> to apple</router-link> // 对象 <router-link :to="{path:'apple'}"> to apple</router-link> // 命名路由 <router