最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue-router 基于后端permissions動(dòng)態(tài)生成導(dǎo)航菜單的示例代碼

 更新時(shí)間:2021年09月30日 11:39:02   作者:Alan_9149  
本文主要介紹了vue-router 基于后端permissions動(dòng)態(tài)生成導(dǎo)航菜單的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

Vue.js

1、注冊(cè)全局守衛(wèi)

核心邏輯
1、token身份驗(yàn)證(后端) => token失效返回登錄頁(yè)面
2、獲取用戶(hù)權(quán)限
3、校驗(yàn)permissions,動(dòng)態(tài)添加路由菜單

router.beforeResolve 注冊(cè)一個(gè)全局守衛(wèi)。和 router.beforeEach 類(lèi)似,區(qū)別是在導(dǎo)航被確認(rèn)之前,同時(shí)在所有組件內(nèi)守衛(wèi)和異步路由組件被解析之后,解析守衛(wèi)就被調(diào)用。

router.beforeResolve(async (to, from, next) => {
  let hasToken = store.getters['User/accessToken']
  if (!settings.loginInterception) hasToken = true
  if (hasToken) {
    if (to.path === '/auth/sign-in') {
      next({ path: '/' })
    } else {
      const hasPermissions =
        store.getters['User/permissions'] &&
        store.getters['User/permissions'].length > 0
      if (hasPermissions) {
        next()
      } else {
        try {
          let permissions
          if (!constant.loginInterception) {
            // settings.js loginInterception為false時(shí),創(chuàng)建虛擬權(quán)限
            await store.dispatch('User/setPermissions', ['admin'])
            permissions = ['admin']
          } else {
            permissions = await store.dispatch('User/getUserInfo')
          }
          let accessRoutes = []
          accessRoutes = await store.dispatch('Routes/setRoutes', permissions)
          // 添加路由
          router.addRoutes(accessRoutes)
          next({ ...to, replace: true })
        } catch {
          await store.dispatch('User/resetAccessToken')
        }
      }
    }
  } else {
    if (settings.routesWhiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      next('/auth/sign-in')
    }
  }
  document.title = getPageTitle(to.meta.title)
})

settings.js 全局設(shè)置

export default {
  // 是否開(kāi)啟登錄攔截
  loginInterception: true,
  // 不經(jīng)過(guò)token校驗(yàn)的路由
  routesWhiteList: ['/auth/sign-in', '/auth/register', '/401', '/404'],
}

2、Vuex狀態(tài)管理 全局緩存routes

  • state :對(duì)數(shù)據(jù)的全局存儲(chǔ)
  • getter: 可以理解為computed ,對(duì)數(shù)據(jù)進(jìn)行計(jì)算
  • mutations :對(duì)數(shù)據(jù)的同步更改
  • actions:對(duì)數(shù)據(jù)的異步更改(實(shí)現(xiàn)異步操作)
  • module: 將 store 分割成模塊
/**
 * @author Alan
 * @description 路由攔截狀態(tài)管理
 */
import { asyncRoutes, constantRoutes } from '@/router'
import { filterAsyncRoutes } from '@/Utils/handleRoutes'

const state = () => ({
  routes: [],
  partialRoutes: []
})
const getters = {
  routes: (state) => state.routes,
  partialRoutes: (state) => state.partialRoutes
}
const mutations = {
  setRoutes (state, routes) {
    state.routes = constantRoutes.concat(routes)
  },

  setPartialRoutes (state, routes) {
    state.partialRoutes = constantRoutes.concat(routes)
  }
}
const actions = {
  async setRoutes ({ commit }, permissions) {
    const finallyAsyncRoutes = await filterAsyncRoutes(
      [...asyncRoutes],
      permissions
    )
    commit('setRoutes', finallyAsyncRoutes)
    return finallyAsyncRoutes
  },
  setPartialRoutes ({ commit }, accessRoutes) {
    commit('setPartialRoutes', accessRoutes)
    return accessRoutes
  }
}
export default { namespaced: true, state, getters, mutations, actions }

3、路由攔截

/**
 * @author Alan
 * @description 判斷當(dāng)前路由是否包含權(quán)限
 * @param permissions
 * @param route
 * @returns {boolean|*}
 */
export function hasPermission (permissions, route) {
  if (route.meta && route.meta.permissions) {
    return permissions.some((role) => route.meta.permissions.includes(role))
  } else {
    return true
  }
}

/**
 * @author Alan
 * @description 根據(jù)permissions數(shù)組攔截路由
 * @param routes
 * @param permissions
 * @returns {[]}
 */
export function filterAsyncRoutes (routes, permissions) {
  const finallyRoutes = []
  routes.forEach((route) => {
    const item = { ...route }
    if (hasPermission(permissions, item)) {
      if (item.children) {
        item.children = filterAsyncRoutes(item.children, permissions)
      }
      finallyRoutes.push(item)
    }
  })
  return finallyRoutes
}

4、路由菜單

/*
* @author Alan
* @description 公共路由
*/
export const constantRoutes = [
  {
    path: '/auth',
    name: 'auth1',
    component: AuthLayout,
    children: authChildRoutes('auth1'),
    hidden: true // 隱藏菜單
  },
  {
    path: '/',
    name: 'dashboard',
    component: VerticleLayout,
    meta: {
      title: 'Dashboard',
      name: 'sidebar.dashboard',
      is_heading: false,
      is_active: false,
      link: '',
      class_name: '',
      is_icon_class: true,
      icon: 'ri-home-4-line',
      permissions: ['admin']
    },
    children: childRoutes('dashboard')
  }
]

/*
* @author Alan
* @description 異步路由
*/
export const asyncRoutes = [
  {
    path: '/menu-design',
    name: 'horizontal-dashboard',
    component: HorizantalLayout,
    meta: {
      title: 'Menu Design',
      name: 'sidebar.MenuDesign',
      is_heading: false,
      is_active: false,
      link: '',
      class_name: '',
      is_icon_class: true,
      icon: 'ri-menu-3-line',
      permissions: ['admin']
    },
    children: horizontalRoute('dashboard')
  }, {
    path: '/core',
    name: 'core',
    component: VerticleLayout,
    meta: {
      title: 'UI Elements',
      name: 'sidebar.uiElements',
      is_heading: false,
      is_active: false,
      class_name: '',
      link: '',
      is_icon_class: true,
      icon: 'ri-pencil-ruler-line',
      permissions: ['admin']
    },
    children: coreChildRoute('core')
  }
]

5、遞歸菜單vue組件

<template>
  <b-collapse tag="ul" :class="className" :visible="open" :id="idName" :accordion="accordianName">
    <li v-for="(item,index) in items" :key="index" :class=" !hideListMenuTitle? 'p-0' : item.meta.is_heading ? 'iq-menu-title' :activeLink(item) && item.children ? 'active' : activeLink(item) ? 'active' : ''">
      <template v-if="!item.hidden">
        <i v-if="item.meta.is_heading && hideListMenuTitle" class="ri-subtract-line" />
        <span v-if="item.meta.is_heading && hideListMenuTitle">{{ $t(item.meta.name) }}</span>
        <router-link :to="item.meta.link" v-if="!item.is_heading" :class="`iq-waves-effect ${activeLink(item) && item.children ? 'active' : activeLink(item) ? 'active' : ''}`" v-b-toggle="item.meta.name">
          <i :class="item.meta.icon" v-if="item.meta.is_icon_class"/>
          <template v-else v-html="item.meta.icon">
          </template>
          <span>{{ $t(item.meta.name) }}</span>
          <i v-if="item.children" class="ri-arrow-right-s-line iq-arrow-right" />
          <small v-html="item.meta.append" v-if="hideListMenuTitle" :class="item.meta.append_class" />
        </router-link>
        <List v-if="item.children" :items="item.children" :sidebarGroupTitle="hideListMenuTitle" :open="item.meta.link.name !== '' && activeLink(item) && item.children ? true : !!(item.meta.link.name !== '' && activeLink(item))" :idName="item.meta.name" :accordianName="`sidebar-accordion-${item.meta.class_name}`" :className="`iq-submenu ${item.meta.class_name}`" />
      </template>
    </li>
  </b-collapse>
</template>
<script>
import List from './CollapseMenu' // 自身組件
import { core } from '../../../config/pluginInit'
export default {
  name: 'List',
  props: {
    items: Array,
    className: { type: String, default: 'iq-menu' },
    open: { type: Boolean, default: false },
    idName: { type: String, default: 'sidebar' },
    accordianName: { type: String, default: 'sidebar' },
    sidebarGroupTitle: { type: Boolean, default: true }
  },
  components: {
    List
  },
  computed: {
    hideListMenuTitle () {
      return this.sidebarGroupTitle
    }
  },
  mounted () {
  },
  methods: {
    activeLink (item) {
      return core.getActiveLink(item, this.$route.name)
    }
  }
}
</script>

到此這篇關(guān)于vue-router 基于后端permissions動(dòng)態(tài)生成導(dǎo)航菜單的示例代碼的文章就介紹到這了,更多相關(guān)vue-router permissions導(dǎo)航菜單內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

  • vue引用public目錄下文件的方式詳解

    vue引用public目錄下文件的方式詳解

    由于一些演示需要對(duì)一些簡(jiǎn)單頁(yè)面進(jìn)行配置,由于打包build后的vue項(xiàng)目基本已經(jīng)看不出原樣,因此需要?jiǎng)?chuàng)建一個(gè)文件,并在打包的時(shí)候不會(huì)進(jìn)行編譯,所以文件放在public,下面這篇文章主要給大家介紹了關(guān)于vue引用public目錄下文件的相關(guān)資料,需要的朋友可以參考下
    2022-08-08
  • 使用vue+element?ui實(shí)現(xiàn)走馬燈切換預(yù)覽表格數(shù)據(jù)

    使用vue+element?ui實(shí)現(xiàn)走馬燈切換預(yù)覽表格數(shù)據(jù)

    這次做項(xiàng)目的時(shí)候遇到需要切換預(yù)覽表格數(shù)據(jù)的需求,所以下面這篇文章主要給大家介紹了關(guān)于使用vue+element?ui實(shí)現(xiàn)走馬燈切換預(yù)覽表格數(shù)據(jù)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-08-08
  • vue3中el-table實(shí)現(xiàn)表格合計(jì)行的示例代碼

    vue3中el-table實(shí)現(xiàn)表格合計(jì)行的示例代碼

    這篇文章主要介紹了vue3中el-table實(shí)現(xiàn)表格合計(jì)行,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2024-01-01
  • vue路由對(duì)不同界面進(jìn)行傳參及跳轉(zhuǎn)的總結(jié)

    vue路由對(duì)不同界面進(jìn)行傳參及跳轉(zhuǎn)的總結(jié)

    這篇文章主要介紹了vue路由對(duì)不同界面進(jìn)行傳參及跳轉(zhuǎn)的總結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • 前端token中4個(gè)存儲(chǔ)位置的優(yōu)缺點(diǎn)說(shuō)明

    前端token中4個(gè)存儲(chǔ)位置的優(yōu)缺點(diǎn)說(shuō)明

    這篇文章主要介紹了前端token中4個(gè)存儲(chǔ)位置的優(yōu)缺點(diǎn)說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • vue學(xué)習(xí)筆記之Vue中css動(dòng)畫(huà)原理簡(jiǎn)單示例

    vue學(xué)習(xí)筆記之Vue中css動(dòng)畫(huà)原理簡(jiǎn)單示例

    這篇文章主要介紹了vue學(xué)習(xí)筆記之Vue中css動(dòng)畫(huà)原理,結(jié)合簡(jiǎn)單實(shí)例形式分析了Vue中css樣式變換動(dòng)畫(huà)效果實(shí)現(xiàn)原理與相關(guān)操作技巧,需要的朋友可以參考下
    2020-02-02
  • vue的滾動(dòng)條插件實(shí)現(xiàn)代碼

    vue的滾動(dòng)條插件實(shí)現(xiàn)代碼

    這篇文章主要介紹了vue的滾動(dòng)條插件實(shí)現(xiàn)代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-09-09
  • vue如何給組件動(dòng)態(tài)綁定不同的事件

    vue如何給組件動(dòng)態(tài)綁定不同的事件

    這篇文章主要介紹了vue如何給組件動(dòng)態(tài)綁定不同的事件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • vue打包通過(guò)image-webpack-loader插件對(duì)圖片壓縮優(yōu)化操作

    vue打包通過(guò)image-webpack-loader插件對(duì)圖片壓縮優(yōu)化操作

    這篇文章主要介紹了vue打包通過(guò)image-webpack-loader插件對(duì)圖片壓縮優(yōu)化操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-11-11
  • vue-cli項(xiàng)目中使用echarts圖表實(shí)例

    vue-cli項(xiàng)目中使用echarts圖表實(shí)例

    在本篇文章里我們給大家分享了關(guān)于vue中使用echarts圖表的實(shí)現(xiàn)方法,有興趣的朋友們學(xué)習(xí)下。
    2018-10-10

最新評(píng)論

天镇县| 大足县| 镇原县| 白银市| 三都| 河津市| 荔浦县| 东莞市| 台前县| 临桂县| 吴川市| 封丘县| 上虞市| 福海县| 贺兰县| 屯留县| 南开区| 喜德县| 富川| 新闻| 佛坪县| 会同县| 育儿| 香格里拉县| 信阳市| 余干县| 广南县| 许昌县| 博野县| 荔波县| 彭州市| 双江| 诏安县| 威宁| 本溪市| 鄄城县| 札达县| 清流县| 天峻县| 延长县| 合肥市|