vue-admin动态路由的实现

47 篇文章 0 订阅

前言:项目开发中菜单栏往往是后端基于角色控制的,所以菜单栏通过后端返回,然后在进行渲染,vue-admin这个管理系统模板用的人贼多,以此为例。

一、首先打开router/index.js文件吗,把constantRoutes写的静态路由全删了,只留下公共的界面,比如login、404之类的。其他路由后面通过接口获取。

二、在store/modules/permission.js进行修改,如果没有文件就创建,该文件使用vuex的状态管理,把菜单信息存储起来,最后把菜单渲染出去

// router/index.js下面定义写死的那个公共界面,用于拼接后端返回的菜单
import { constantRoutes } from '@/router'
// api接口
import { getRouter } from '@/api/user'
const state = {
  routes: [], //最后拼接完成的路由,需要暴露出去
  addRoutes: [] //后端返回的路由
}
const mutations = {
  SET_ROUTES: (state, routes) => {
    state.addRoutes = routes
    state.routes = constantRoutes.concat(routes)
  }
}
const actions = {
  generateRoutes({ commit }, roles) {
    return new Promise(resolve => {
      // getRouter就是请求后端的接口
      commit('SET_ROUTES', getRouter())
      resolve(getRouter())
    })
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}

  permission_routes: state => state.permission.routes

import permission from './modules/permission'

三,修改src目录下的permission.js,这里主要是在路由拦截那里进行路由菜单的获取。

修改如下:

store.dispatch('permission/generateRoutes').then(newRouter => {
  router.addRoutes(filterRouter(newRouter))
  next({ ...to, replace: true })
})

遍历处理返回的router

import Layout from '@/layout'

const filterRouter = (router) => {
  const res = []
  router.forEach(a => {
    if (a.component === 'Layout') {
      a.component = Layout
    } else {
      a.component = loadComponent(a.component)
    }
    if (a.children && a.children.length > 0) {
      a.children = filterRouter(a.children)
    }
    res.push(a)
  })
  return res
}

const loadComponent = (path) => {
  // 路由懒加载(参数不能头前缀"/")
  return (resolve) => require([`@/views/${path}.vue`], resolve) // 这里的path='table/index'
}

注意:静态路由中,component这里是这样的

后端返回的确是"Layout"字符串,和url字符串(注意,url字符串不能以下划线 / 开头,假如静态路由地址是component: () => import(’@/views/table/index’)

那么后端返回的地址必须是 component:“table/index”,前面的**@/views/**前端拼接,因为这里在使用require进行路由懒加载的时候才不会有问题。

四、修改导航菜单的渲染部分,在src/layout/components/Sidebar/index.vue中修改

<!-- 动态路由 -->
<sidebar-item v-for="route in permission_routes" :key="route.path" :item="route" :base-path="route.path" />

permission_routes

就可以喽。。。。。

Vue-element-admin是一个基于Vue.js的后台管理系统解决方案。它采用了动态路由实现方式,可以根据用户权限动态生成菜单和路由。具体的实现步骤如下: 1. 在后端将用户的权限信息返回给前端,包括路由信息和菜单信息。 2. 在前端将路由信息存储在vuex的state中,可以通过store.dispatch()将数据传递到state中。 3. 通过Vue-router提供的addRoutes()方法,根据用户权限动态生成路由。 4. 在菜单中显示对应的可用路由。 下面是一个简单的示例: 在后端返回的路由信息中包含如下格式的数据: ``` { path: '/dashboard', component: Layout, redirect: '/dashboard/index', children: [ { path: 'index', name: 'Dashboard', component: () => import('@/views/dashboard/index'), meta: { title: 'Dashboard', icon: 'dashboard' } } ] } ``` 在前端可以通过以下代码将路由信息存储到vuex中: ``` import { asyncRoutes, constantRoutes } from '@/router' const state = { routes: [], // 存储最终可用的路由 addRoutes: [] // 存储根据用户权限动态生成的路由 } const mutations = { SET_ROUTES: (state, routes) => { state.addRoutes = routes state.routes = constantRoutes.concat(routes) } } const actions = { generateRoutes({ commit }, roles) { return new Promise(resolve => { let accessedRoutes if (roles.includes('admin')) { accessedRoutes = asyncRoutes || [] } else { accessedRoutes = filterAsyncRoutes(asyncRoutes, roles) } commit('SET_ROUTES', accessedRoutes) resolve(accessedRoutes) }) } } ``` 通过调用store.dispatch()方法,可以将数据传递到vuex中: ``` store.dispatch('generateRoutes', ['admin']).then(() => { router.addRoutes(store.getters.addRoutes) }) ``` 上述代码中,先调用generateRoutes()方法来根据用户权限生成路由,然后再调用addRoutes()方法将动态生成的路由添加到router中。这样就可以根据用户权限动态生成菜单和路由了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值