vue-admin-template 动态路由的实现(方式一)

hidden: true,

component: Layout,

children: [

{

path: ‘index/:id’,

name: ‘AddBanner’,

component: () => import(‘@/views/appmanage/addbanner/index’),

meta: { title: ‘添加Banner’, icon: ‘’ }

}

]

},

{

path: ‘/createactivationcode’,

hidden: true,

component: Layout,

children: [

{

path: ‘index’,

name: ‘CreateActivationCode’,

component: () => import(‘@/views/vipmanage/createactivationcode/index’),

meta: { title: ‘生成激活码’, icon: ‘’ }

}

]

},

]

/**

  • 放置服务器可配置的动态路由

*/

export const asyncRoutes = [

{

path: ‘/usermanagement’,

component: Layout,

redirect: ‘/usermanagement/rolemanagement’,

meta: { title: ‘用户管理’, icon: ‘user’ },

children: [

{

path: ‘rolemanagement’,

component: () => import(‘@/views/usermanage/rolemanagement/index’),

meta: { title: ‘角色管理’, icon: ‘’ }

},

{

path: ‘accountlist’,

component: () => import(‘@/views/usermanage/accountlist/index’),

meta: { title: ‘账号列表’, icon: ‘’ }

}

]

},

{

path: ‘/voicemanage’,

component: Layout,

redirect: ‘/voicemanage/albumscategories’,

meta: { title: ‘语音管理’, icon: ‘voice’ },

children: [

{

path: ‘albumscategories’,

component: () => import(‘@/views/voicemanage/albumscategories/index’),

meta: { title: ‘专辑分类’, icon: ‘’ }

},

{

path: ‘listenmanage’,

component: () => import(‘@/views/voicemanage/listenmanage/index’),

meta: { title: ‘收听管理’, icon: ‘’ }

}

]

},

{

path: ‘/appmanagement’,

component: Layout,

redirect: ‘/appmanagement/banner’,

meta: { title: ‘App管理’, icon: ‘app’ },

children: [

{

path: ‘banner’,

component: () => import(‘@/views/appmanage/banner/index’),

meta: { title: ‘Banner管理’, icon: ‘’ }

},

{

path: ‘appversions’,

component: () => import(‘@/views/appmanage/appversions/index’),

meta: { title: ‘App版本管理’, icon: ‘’ }

}

]

},

{

path: ‘/vipmanagement’,

component: Layout,

redirect: ‘/vipmanagement/setmeal’,

meta: { title: ‘VIP管理’, icon: ‘vip’ },

children: [

{

path: ‘setmeal’,

component: () => import(‘@/views/vipmanage/setmeal/index’),

meta: { title: ‘套餐管理’, icon: ‘’ }

},

{

path: ‘activationcode’,

component: () => import(‘@/views/vipmanage/activationcode/index’),

meta: { title: ‘激活码管理’, icon: ‘’ }

},

{

path: ‘activationcodelist’,

component: () => import(‘@/views/vipmanage/activationcodelist/index’),

meta: { title: ‘激活码生成列表’, icon: ‘’ }

},

{

path: ‘activationcodeexport’,

component: () => import(‘@/views/vipmanage/activationcodeexport/index’),

meta: { title: ‘激活码导出列表’, icon: ‘’ }

},

{

path: ‘orderlist’,

component: () => import(‘@/views/vipmanage/orderlist/index’),

meta: { title: ‘订单列表’, icon: ‘’ }

},

{

path: ‘vipequity’,

component: () => import(‘@/views/vipmanage/vipequity/index’),

meta: { title: ‘VIP权益说明’, icon: ‘’ }

}

]

},

{

path: ‘/brandownermanage’,

component: Layout,

redirect: ‘/brandownermanage/brandownerlist’,

alwaysShow: true,

meta: { title: ‘品牌商管理’, icon: ‘brand’ },

children: [

{

path: ‘brandownerlist’,

component: () => import(‘@/views/brandownermanage/brandownerlist/index’),

meta: { title: ‘品牌商列表’, icon: ‘’ }

}

]

},

{

path: ‘/machinemanage’,

component: Layout,

redirect: ‘/machinemanage/machinelist’,

alwaysShow: true,

meta: { title: ‘机器管理’, icon: ‘android’ },

children: [

{

path: ‘machinelist’,

component: () => import(‘@/views/machinemanage/machinelist/index’),

meta: { title: ‘机器列表’, icon: ‘’ }

}

]

},

{

path: ‘/softwaremanage’,

component: Layout,

redirect: ‘/softwaremanage/versions’,

meta: { title: ‘软件管理’, icon: ‘software’ },

children: [

{

path: ‘versions’,

component: () => import(‘@/views/softwaremanage/versions/index’),

meta: { title: ‘版本管理’, icon: ‘’ }

},

{

path: ‘usedirection’,

component: () => import(‘@/views/softwaremanage/usedirection/index’),

meta: { title: ‘使用说明’, icon: ‘’ }

}

]

},

{

path: ‘/officialaccounts’,

component: Layout,

redirect: ‘/officialaccounts/accesssettings’,

meta: { title: ‘公众号设置’, icon: ‘weichat’ },

children: [

{

path: ‘accesssettings’,

component: () => import(‘@/views/officialaccounts/accesssettings/index’),

meta: { title: ‘接入设置’, icon: ‘’ }

},

{

path: ‘menu’,

component: () => import(‘@/views/officialaccounts/menu/index’),

meta: { title: ‘菜单设置’, icon: ‘’ }

},

{

path: ‘attentionreply’,

component: () => import(‘@/views/officialaccounts/attentionreply/index’),

meta: { title: ‘关注回复’, icon: ‘’ }

},

{

path: ‘keywordreply’,

component: () => import(‘@/views/officialaccounts/keywordreply/index’),

meta: { title: ‘关键字回复’, icon: ‘’ }

},

{

path: ‘material’,

component: () => import(‘@/views/officialaccounts/material/index’),

meta: { title: ‘素材管理’, icon: ‘’ }

}

]

},

]

const createRouter = () => new Router({

// mode: ‘history’, // require service support

scrollBehavior: () => ({ y: 0 }),

routes: constantRoutes.concat(asyncRoutes).concat(errorRoutes)

})

export const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465

export function resetRouter() {

const newRouter = createRouter()

router.matcher = newRouter.matcher // reset router

}

export default router

  • 获取动态路由数据api/user.js

import request from ‘@/utils/request’

import qs from ‘qs’

/**

*动态路由

*/

export function getAuthMenu() {

return request({

url: ‘/Admin/Index’,

method: ‘get’

})

}

  • 修改权限规则(store/module/permission.js)

vue-admin-template模板本身是没有做权限的,自己创建一个即可,规则自己设定,admin中使用的roles配置的,我这里是自动生成的,本质还是生成新的路由list(只是包含新的路由)

注意:store/module/permission.js和项目根目录下的permission别搞混了

import { asyncRoutes, constantRoutes, errorRoutes } from ‘@/router’

import { getAuthMenu } from ‘@/api/user’

import Layout from ‘@/layout’

import { Message } from “element-ui”

/**

  • 后台查询的菜单数据拼装成路由格式的数据

  • 这里是demo使用的是以前项目的后台数据,这里强制修改了一下

  • @param routes

*/

export function generaMenu(routes, data, parent) {

data.forEach(item => {

let menu

let viewpath

if (parent == “/usermanagement”) {

viewpath = “/usermanage”

} else if (parent == “/appmanagement”) {

viewpath = “/appmanage”

} else if (parent == “/vipmanagement”) {

viewpath = “/vipmanage”

} else {

viewpath = parent

}

if (parent === ‘#’) {

menu = {

path: item.path,

component: Layout,

children: [],

redirect: ${item.path}/${item.children[0].path},

meta: { title: item.name, icon: item.icon }

}

} else {

menu = {

path: item.path,

component: loadView(viewpath, item.path),

children: [],

name: item.path,

meta: { title: item.name }

}

}

if (item.children) {

generaMenu(menu.children, item.children, item.path)

}

routes.push(menu)

})

}

export const loadView = (parent, path) => { // 路由懒加载

return (resolve) => require([@/views${parent}/${path}/index], resolve)

// return () => import(@/views${parent}/${path}/index)

}

const state = {

routes: [],

addRoutes: []

}

const mutations = {

SET_ROUTES: (state, routes) => {

state.addRoutes = routes

state.routes = constantRoutes.concat(routes).concat(errorRoutes)

}

}

const actions = {

generateRoutes({ commit }, roles) {

return new Promise(resolve => {

const loadMenuData = []

// 先查询后台并返回左侧菜单数据并把数据添加到路由

getAuthMenu().then(response => {

if (response.code !== 200) {

Message({

message: “菜单数据加载异常”,

type: 0,

duration: 2 * 1000

});

} else {

let data = response.data.menu_list

Object.assign(loadMenuData, data)

let routes=[]

generaMenu(routes, loadMenuData, “#”)

let accessedRoutes

if (roles.includes(‘admin’)) {

accessedRoutes = asyncRoutes || []

} else {

accessedRoutes = routes

}

commit(‘SET_ROUTES’, accessedRoutes)

resolve(accessedRoutes)

}

}).catch(error => {

console.log(error)

})

})

},

}

export default {

namespaced: true,

state,

mutations,

actions

}

  • 配置项目根目录下的permission.js,实现路由筛选

import router from ‘./router’

import store from ‘./store’

import { Message } from ‘element-ui’

import NProgress from ‘nprogress’ // progress bar

import ‘nprogress/nprogress.css’ // progress bar style

import { getToken } from ‘@/utils/auth’ // get token from cookie

import getPageTitle from ‘@/utils/get-page-title’

NProgress.configure({ showSpinner: false }) // NProgress Configuration

//动态路由添加’/auth-redirect’

const whiteList = [‘/login’] // no redirect whitelist

router.beforeEach(async (to, from, next) => {

// start progress bar

NProgress.start()

// set page title

document.title = getPageTitle(to.meta.title)

// determine whether the user has logged in

const hasToken = getToken()

if (hasToken) {

if (to.path === ‘/login’) {

// if is logged in, redirect to the home page

next({ path: ‘/’ })

NProgress.done()

} else {

// const hasGetUserInfo = store.getters.name

//动态路由

const hasGetUserInfo = store.getters.name&&store.getters.permission_routes.length > 0

if (hasGetUserInfo) {

next()

} else {

try {

// get user info

// await store.dispatch(‘user/getInfo’)

// next()

//配置动态路由

const { contact_name } = await store.dispatch(‘user/getInfo’)

const accessRoutes = await store.dispatch(‘permission/generateRoutes’, contact_name)

router.addRoutes(accessRoutes)

next({ …to, replace: true })

} catch (error) {

// remove token and go to login page to re-login

await store.dispatch(‘user/resetToken’)

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
utes’, contact_name)

router.addRoutes(accessRoutes)

next({ …to, replace: true })

} catch (error) {

// remove token and go to login page to re-login

await store.dispatch(‘user/resetToken’)

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

[外链图片转存中…(img-ycU9BERp-1715657856168)]

[外链图片转存中…(img-cEaaRYHA-1715657856168)]

[外链图片转存中…(img-57TDet8I-1715657856168)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值