亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

使用vue-element-admin框架從后端動(dòng)態(tài)獲取菜單功能的實(shí)現(xiàn)

 更新時(shí)間:2021年04月29日 10:21:06   作者:#Empty  
​ vue-element-admin是一個(gè)純前端的框架,左側(cè)菜單是根據(jù)路由生成的。實(shí)際開(kāi)發(fā)中經(jīng)常需要根據(jù)當(dāng)前登陸人員的信息從后端獲取菜單進(jìn)行展示,本文將詳細(xì)介紹如何實(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)文章

最新評(píng)論