第一种 后台返回的是多维数组,即菜单格式以及排好
Vue.prototype.routerLoad = {
bianli(arr) {
return arr.map(v => {
if (v.subs) {
return this.bianli(v.subs);
} else {
return {
path: "/" + v.index,
name: v.index,
component: () =>import (`@/components/page/${v.index}/${v.index}.vue`),
meta: { title: v.title }
}
}
})
},
loadMenus() {
let userId = sessionStorage.getItem('userId')
if (userId) {
axios.get(`/api/permission/role/functions/${userId}`).then(res => {
console.log('动态路由添加成功')
console.log(res)
let menus = res.data;
store.commit('setMenus', menus)
let routerArr = this.bianli(menus).flat(9);
router.options.routes[1].children.push(...routerArr);
router.addRoutes(router.options.routes);
if (sessionStorage.getItem('path') == '/') {
sessionStorage.setItem('path', '/dashboard')
}
router.push({ path: sessionStorage.getItem('path') })
})
.catch(err => {
console.log("网络错误");
});
}
}
}
router.beforeEach((to, from, next) => {
if (to.path != '/404' && to.path != "/403") {
sessionStorage.setItem('path', to.fullPath)
}
})
new Vue({
router,
store,
render: h => h(App),
created() {
if (sessionStorage.getItem('token')) {
this.routerLoad.loadMenus()
}
}
}).$mount('#app')
-------------------------------------------------------------------------------
sessionStorage.setItem('token',123465)
this.routerLoad.loadMenus()
this.$router.push('/dashboard')
-------------------------------------------------------------------------------
import Vue from "vue"
import Vuex from 'vuex'
Vue.use(Vuex);
const store = new Vuex.Store({
state: {
menus: []
},
mutations: {
setMenus(state, menus) {
state.menus = menus
}
},
actions: {
},
getters: {
}
});
export default store
第二种 后台返回的是一维数组,即通过里面的父级Id值来遍历判断
Vue.prototype.routerLoad = {
filterArr(arr, i) {
let obj = undefined
arr.find(item => {
if (item.id == i) {
obj = item
return true
} else if (item.subs) {
obj = this.filterArr(item.subs, i)
if (obj) {
return obj
}
}
})
return obj
},
arr(arr, roleTree, roleTreeObj) {
arr.find((item, index) => {
let itemObj = {
icon: item.iocn,
index: item.urlPre,
title: item.name,
id: item.id,
sort: item.sort,
superior: item.superior
}
if (item.id == item.superior) {
roleTree.push(itemObj)
} else {
let obj = this.filterArr(roleTree, item.superior)
if (!obj) {
if (!roleTreeObj[item.superior]) {
roleTreeObj[item.superior] = []
}
roleTreeObj[item.superior].push(itemObj)
} else {
if (!Array.isArray(obj.subs)) {
obj.subs = []
}
obj.subs.push(itemObj)
}
}
})
return { roleTree, roleTreeObj }
},
binaliObj(allObj) {
for (let key in allObj.roleTreeObj) {
let obj = this.filterArr(allObj.roleTree, key)
if (obj) {
if (!Array.isArray(obj.subs)) {
obj.subs = []
}
obj.subs.push(...allObj.roleTreeObj[key])
delete allObj.roleTreeObj[key]
}
}
if (Object.keys(allObj.roleTreeObj).length != 0) {
this.binaliObj(allObj)
}
return false
},
sortArr(arr) {
arr.sort(function(a, b) {
if (a.sort > b.sort) {
return 1;
} else if (a.sort == b.sort) {
return 0;
} else {
return -1;
}
})
arr.forEach(item => {
if (item.subs) {
this.sortArr(item.subs)
}
});
},
bianli(arr) {
return arr.map(v => {
if (v.subs) {
return this.bianli(v.subs);
} else {
return {
path: v.index,
name: v.index.slice(1),
component: () => {
if (v.index == '/Dashboard') {
return import (`@/components/page${v.index}.vue`)
} else if (v.index == '/SI') {
return import (`@/components/page/Sim/Sim.vue`)
} else {
return import (`@/components/page${v.index}${v.index}.vue`)
}
},
meta: { title: v.title }
}
}
})
},
loadMenus() {
axios.get(`/api/common/v1.0/getMenu`).then(res => {
console.log('动态获取成功')
let roleTree = []
let allObj = this.arr(res.data.data, roleTree, {})
console.log(allObj)
this.binaliObj(allObj)
this.sortArr(allObj.roleTree)
store.commit('setMenus', allObj.roleTree)
let routerArr = this.bianli(allObj.roleTree).flat(9)
router.options.routes[1].children.push(...routerArr);
router.addRoutes(router.options.routes);
console.log(router)
if (sessionStorage.getItem('path') == '/') {
sessionStorage.setItem('path', '/dashboard')
}
router.push({ path: sessionStorage.getItem('path') })
})
.catch(err => {
alert("菜单错误");
});
}
}
router.beforeEach((to, from, next) => {
if (to.path != '/404' && to.path != "/403") {
sessionStorage.setItem('path', to.fullPath)
}
})
new Vue({
router,
store,
render: h => h(App),
created() {
if (sessionStorage.getItem('token')) {
this.routerLoad.loadMenus()
}
}
}).$mount('#app')
-------------------------------------------------------------------------------
sessionStorage.setItem('token',123465)
this.routerLoad.loadMenus()
this.$router.push('/')
-------------------------------------------------------------------------------
import Vue from "vue"
import Vuex from 'vuex'
Vue.use(Vuex);
const store = new Vuex.Store({
state: {
menus: []
},
mutations: {
setMenus(state, menus) {
state.menus = menus
}
},
actions: {
},
getters: {
}
});
export default store
最后菜单列表渲染页面用 this.$store.state.menus 获取菜单列表就行