使用vue-element-admin框架從后端動(dòng)態(tài)獲取菜單功能的實(shí)現(xiàn)
2、詳解
整體思路為:登陸 > 成功后根據(jù)用戶(hù)信息獲取菜單 > 根據(jù)菜單生成路由信息
2.1、新增asyncRoutes路由
在vue-router路徑src\router\index.js
中新增asyncRoutes數(shù)組,用來(lái)存放后端獲取的菜單對(duì)應(yīng)的路由信息。
export const asyncRoutes = [ { path: '*', redirect: '/404', hidden: true } ]
constantRoutes和asyncRoutes的區(qū)別
constantRoutes:不需要?jiǎng)討B(tài)判斷權(quán)限的路由,如登錄頁(yè)、404等通用頁(yè)面。
asyncRoutes:需求動(dòng)態(tài)判斷權(quán)限并通過(guò)addRoutes動(dòng)態(tài)添加的頁(yè)面
2.2、新建permission.js文件
在vuex路徑src\store\modules\permission.js
下新建permission.js文件,該操作為最重要的一步,主要是從后端查詢(xún)菜單并生成路由。
import { asyncRoutes, constantRoutes } from '@/router' import { fetchUserMenuList } from '@/api/user' import Layout from '@/layout' /** * 靜態(tài)路由懶加載 * @param view 格式必須為 xxx/xxx 開(kāi)頭不要加斜杠 * @returns */ export const loadView = (view) => { return (resolve) => require([`@/views/${view}.vue`], resolve) } /** * 把從后端查詢(xún)的菜單數(shù)據(jù)拼裝成路由格式的數(shù)據(jù) * @param routes * @param data 后端返回的菜單數(shù)據(jù) */ export function generaMenu(routes, data) { data.forEach(item => { const menu = { path: item.url, component: item.component === '#' ? Layout : loadView(item.component), hidden: item.status === 0, // 狀態(tài)為0的隱藏 redirect: item.redirect, children: [], name: item.code, meta: item.meta } if (item.children) { generaMenu(menu.children, item.children) } routes.push(menu) }) return routes } const state = { routes: [], addRoutes: [] } const mutations = { SET_ROUTES: (state, routes) => { state.addRoutes = routes // 拼接靜態(tài)路由和動(dòng)態(tài)路由 state.routes = constantRoutes.concat(routes) } } const actions = { generateRoutes({ commit }, token) { return new Promise(resolve => { // 通過(guò)token從后端獲取用戶(hù)菜單,并加入全局狀態(tài) fetchUserMenuList(token).then(res => { const menuData = Object.assign([], res.data) const tempAsyncRoutes = Object.assign([], asyncRoutes) const accessedRoutes = generaMenu(tempAsyncRoutes, menuData) commit('SET_ROUTES', accessedRoutes) resolve(accessedRoutes) }).catch(error => { console.log(error) }) }) } } export default { namespaced: true, state, mutations, actions }
2.3、在vuex中注冊(cè)permission模塊
如果使用的是vue-element-admin請(qǐng)?zhí)^(guò)此步,因?yàn)樗?code>src\store\index.js中自動(dòng)注冊(cè)了src\store\modules
下的所有模塊。如果你使用的是vue-element-template,可以參考admin,將index.js文件改造一下,也可以手動(dòng)import一下。
import Vue from 'vue' import Vuex from 'vuex' import getters from './getters' Vue.use(Vuex) // https://webpack.js.org/guides/dependency-management/#requirecontext const modulesFiles = require.context('./modules', true, /\.js$/) // you do not need `import app from './modules/app'` // it will auto require all vuex module from modules file const modules = modulesFiles.keys().reduce((modules, modulePath) => { // set './app.js' => 'app' const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1') const value = modulesFiles(modulePath) modules[moduleName] = value.default return modules }, {}) const store = new Vuex.Store({ modules, getters }) export default store
2.4、在getters中增加路由狀態(tài)
在vuex路徑src\store\getters.js
添加menusRoutes狀態(tài)
menusRoutes: state => state.permission.routes
2.5、修改菜單生成數(shù)據(jù)來(lái)源
在路徑src\layout\components\Sidebar\index.vue
修改routes數(shù)據(jù)來(lái)源,原來(lái)數(shù)據(jù)源是路由,改為從vuex中獲取。
routes() { // return this.$router.options.routes return this.$store.getters.menusRoutes },
至此,從后端獲取菜單數(shù)據(jù)到頁(yè)面展示的邏輯已經(jīng)完畢,下面開(kāi)始在登陸后進(jìn)行調(diào)用。
2.6、登陸后獲取菜單
在vuex路徑src\store\modules\user.js
的login方法中,加入登陸成功通過(guò)token獲取菜單生成路由邏輯。
// 獲取菜單,調(diào)用其他文件中actions時(shí)必須加 { root: true } dispatch('permission/generateRoutes', data, { root: true }).then((accessRoutes) => { router.addRoutes(accessRoutes) })
2.7、解決刷新后頁(yè)面空白
以上內(nèi)容已經(jīng)可以實(shí)現(xiàn)登陸后展示左側(cè)菜單功能,但是會(huì)發(fā)現(xiàn)每次刷新頁(yè)面后,頁(yè)面都會(huì)變空白。這是因?yàn)樵陧?yè)面刷新時(shí),會(huì)重新加載vue實(shí)例,vuex的store中的數(shù)據(jù)會(huì)被重新賦值,導(dǎo)致我們存在vuex中的路由信息被清空。
在src\permission.js
中增加重新獲取路由代碼。
const accessRoutes = await store.dispatch('permission/generateRoutes', store.getters.token) router.addRoutes(accessRoutes) next({ ...to, replace: true })
3、總結(jié)
至此根據(jù)用戶(hù)信息動(dòng)態(tài)獲取菜單內(nèi)容已經(jīng)全部完成。
到此這篇關(guān)于使用vue-element-admin框架從后端動(dòng)態(tài)獲取菜單的文章就介紹到這了,更多相關(guān)vue-element-admin動(dòng)態(tài)獲取菜單內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue實(shí)現(xiàn)模糊查詢(xún)filter()實(shí)例詳解
因?yàn)榻赵趯W(xué)習(xí)并使用VUE,客戶(hù)有一個(gè)要求,要輸入框可模糊查詢(xún)并帶有下拉提示的應(yīng)用,數(shù)據(jù)從接口取,下面這篇文章主要給大家介紹了關(guān)于Vue實(shí)現(xiàn)模糊查詢(xún)filter()的相關(guān)資料,需要的朋友可以參考下2023-04-04Vue組件之極簡(jiǎn)的地址選擇器的實(shí)現(xiàn)
這篇文章主要介紹了Vue組件之極簡(jiǎn)的地址選擇器的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05vue單頁(yè)應(yīng)用加百度統(tǒng)計(jì)代碼(親測(cè)有效)
這篇文章主要介紹了vue單頁(yè)應(yīng)用加百度統(tǒng)計(jì)代碼的解決方法,需要的朋友參考下吧2018-01-01vue.js實(shí)現(xiàn)點(diǎn)擊圖標(biāo)放大離開(kāi)時(shí)縮小的代碼
這篇文章主要介紹了vue.js實(shí)現(xiàn)點(diǎn)擊圖標(biāo)放大離開(kāi)時(shí)縮小,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01