欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

關于vue中根據(jù)用戶權限動態(tài)添加路由的問題

 更新時間:2021年11月05日 10:52:46   作者:一只燭  
每次路由發(fā)生變化時都需要調用一次路由守衛(wèi),并且store中的數(shù)據(jù)會在每次刷新的時候清空,因此需要判斷store中是否有添加的動態(tài)路由,本文給大家分享vue中根據(jù)用戶權限動態(tài)添加路由的問題,感興趣的朋友一起看看吧

根據(jù)用戶的權限,展示不同的菜單頁。

知識點

路由守衛(wèi)(使用了前置守衛(wèi)):根據(jù)用戶角色判斷要添加的路由
vuex:保存動態(tài)添加的路由

難點

每次路由發(fā)生變化時都需要調用一次路由守衛(wèi),并且store中的數(shù)據(jù)會在每次刷新的時候清空,因此需要判斷store中是否有添加的動態(tài)路由。
(若沒有判斷 則會一直添加 導致內(nèi)存溢出)

在這里插入圖片描述

根據(jù)角色判斷路由
過濾動態(tài)路由 判斷每條路由角色是否與登錄傳入的角色一致

在這里插入圖片描述

<template>
  <div>
    <el-menu
      :default-active="$route.path"
      class="el-menu-vertical-demo menu_wrap"
      background-color="#324057"
      text-color="white"
      active-text-color="#20a0ff"
      :collapse="isCollapse"
      unique-opened
      router
    >
      <el-submenu
        v-for="item in $store.state.Routers"
        :key="item.path"
        :index="item.path"
        v-if="!item.hidden"
      >
        <template slot="title" >
          <i class="el-icon-location"></i>
          <span>{{ item.meta.title }}</span>
        </template>
        <div v-for="chi in item.children" :key="chi.name">
          <el-menu-item v-if="!chi.hidden" :index="item.path + '/' + chi.path">
            <i class="el-icon-location"></i>{{ chi.meta.title }}
          </el-menu-item>
        </div>
      </el-submenu>
    </el-menu>
  </div>
</template>

<script>
export default {
  name: "MenuList",
  data() {
    return {
      isCollapse: false,
    };
  },
  created() {
    this.$bus.$on("getColl", (data) => {
      this.isCollapse = data;
    });
  },
  methods: {

  }
};
</script>

<style scoped>
.menu_wrap {
  height: 100vh;
}
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  height: 100vh;
}
</style>
import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store/index'

Vue.use(VueRouter)

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

export const routes = [
  {
    path: '/home',
    name: 'First',
    component: () => import('../views/Index.vue'),
    meta: { title: 'Home'},
    children: [
      {
        path: 'index',
        name: 'Home',
        component: () => import('../views/Home'),
        meta: { title: 'Home', roles: ['Customer'] }
      }
    ]
  },
  {
    path: '/index',
    name: 'NavigationOne',
    component: () => import('../views/Index.vue'),
    meta: { title: '導航一'},
    children: [
      {
        path: 'personnel',
        name: 'Personnel ',
        component: () => import('../views/One/Personnel.vue'),
        meta: { title: 'Personnel', roles: ['Customer'] }
      },
      {
        path: 'account',
        name: 'Account',
        component: () => import('../views/One/Account.vue'),
        meta: { title: 'Account', roles: ['Customer'] }
      },
      {
        path: 'psw',
        name: 'psw',
        component: () => import('../views/One/Password.vue'),
        meta: { title: 'psw', roles: ['Customer'] }
      }
    ]
  },
  {
    path: '/card',
    name: 'NavigationTwo',
    component: () => import('../views/Index.vue'),
    meta: { title: '導航二'},
    children: [
      {
        path: 'activity',
        name: 'Activity ',
        component: () => import('../views/Three/Activity.vue'),
        meta: { title: 'Activity', roles: ['Customer'] }
      },
      {
        path: 'Social',
        name: 'Social',
        component: () => import('../views/Three/Social.vue'),
        meta: { title: 'Social', roles: ['Customer'] }
      },
      {
        path: 'content',
        name: 'Content',
        component: () => import('../views/Three/Content.vue'),
        meta: { title: 'Content', roles: ['Customer'] }
      }
    ]
  },
  {
    path: '/two',
    name: 'NavigationThree',
    component: () => import('../views/Index.vue'),
    meta: { title: '導航三'},
    children: [
      {
        path: 'index',
        name: 'Two ',
        component: () => import('../views/Two'),
        meta: { title: 'Two', roles: ['Customer'] }
      }]
  },
  {
    path: '/404',
    name: 'Error',
    hidden: true,
    meta: { title: 'error'},
    component: () => import('../views/Error')
  }
]

export const asyncRouter = [
  // Agent3 Staff2
  {
    path: '/agent',
    component: () => import('../views/Index.vue'),
    name: 'Agent',
    meta: { title: 'Agent', roles: ['Agent','Staff']},
    children: [
      {
        path: 'one',
        name: 'agentOne',
        component: () => import('@/views/agent/One'),
        meta: { title: 'agentOne', roles: ['Agent','Staff']  }
      },
      {
        path: 'two',
        name: 'agentTwo',
        component: () => import('@/views/agent/Two'),
        meta: { title: 'agentTwo', roles: ['Agent']  }
      },
      {
        path: 'three',
        name: 'agentThree',
        component: () => import('@/views/agent/Three'),
        meta: { title: 'agentThree', roles: ['Agent','Staff']  }
      }
    ]
  },
  // Staff3
  {
    path: '/staff',
    component: () => import('../views/Index.vue'),
    name: 'Staff',
    meta: { title: 'Staff', roles: ['Staff']},
    children: [
      {
        path: 'one',
        name: 'StaffOne',
        component: () => import('@/views/Staff/One'),
        meta: { title: 'StaffOne', roles: ['Staff']  }
      },
      {
        path: 'two',
        name: 'StaffTwo',
        component: () => import('@/views/Staff/Two'),
        meta: { title: 'StaffTwo', roles: ['Staff']  }
      },
      {
        path: 'three',
        name: 'StaffThree',
        component: () => import('@/views/Staff/Three'),
        meta: { title: 'StaffThree', roles: ['Staff']  }
      }
    ]
  },
  { path: '*', redirect: '/404', hidden: true }
]

const router = new VueRouter({
  routes
})


router.beforeEach((to, from, next) =>{
  let roles = ['Staff']
  if(store.state.Routers.length) {
    console.log('yes')
    next()
  } else {
    console.log('not')
    store.dispatch('asyncGetRouter', {roles})
    .then(res =>{
      router.addRoutes(store.state.addRouters)
    })
    next({...to})
    // next()與next({ ...to })的區(qū)別:next() 放行   next('/XXX') 無限攔截
  }
})

export default router

import Vue from 'vue'
import Vuex from 'vuex'
import modules from './module'

import router, {routes, asyncRouter} from '../router'

function hasPermission(route, roles) {
  if(route.meta && route.meta.roles) {
    return roles.some(role =>route.meta.roles.indexOf(role) >= 0)
  } else {
    return true
  }
  
}

/*
  遞歸過濾異步路由表 返回符合用戶角色的路由
  @param asyncRouter 異步路由
  @param roles 用戶角色
*/
function filterAsyncRouter(asyncRouter, roles) {
  let filterRouter = asyncRouter.filter(route =>{
    if(hasPermission(route, roles)) {
      if(route.children && route.children.length) {
          route.children = filterAsyncRouter(route.children, roles)
      }
      return true 
    }
    return false
  })
  return filterRouter
}

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    addRouters:  [],
    Routers: []
  },
  mutations: {
    getRouter(state, paload) {
      // console.log(paload)
      state.Routers = routes.concat(paload)
      state.addRouters = paload
      // router.addRoutes(paload)
    }
  },
  actions: {
    asyncGetRouter({ commit }, data) {
      const { roles } = data
      return new Promise(resolve =>{
        let addAsyncRouters = filterAsyncRouter(asyncRouter, roles)
        commit('getRouter', addAsyncRouters)
        resolve()
      })
    }
  }
})

到此這篇關于vue中根據(jù)用戶權限動態(tài)添加路由詳解的文章就介紹到這了,更多相關vue動態(tài)添加路由內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Vue中select下拉框的默認選中項的三種情況解讀

    Vue中select下拉框的默認選中項的三種情況解讀

    這篇文章主要介紹了Vue中select下拉框的默認選中項的三種情況解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • Vue2 配置 Axios api 接口調用文件的方法

    Vue2 配置 Axios api 接口調用文件的方法

    本篇文章主要介紹了Vue2 配置 Axios api 接口調用文件的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • vue 使用v-for進行循環(huán)的實例代碼詳解

    vue 使用v-for進行循環(huán)的實例代碼詳解

    這篇文章主要介紹了vue 使用v-for進行循環(huán)的實例代碼詳解,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-02-02
  • vue 組件數(shù)據(jù)加載解析順序的詳細代碼

    vue 組件數(shù)據(jù)加載解析順序的詳細代碼

    Vue.js的解析順序可以概括為:模板編譯、組件創(chuàng)建、數(shù)據(jù)渲染、事件處理和生命周期鉤子函數(shù)執(zhí)行,接下來通過本文給大家介紹vue 組件數(shù)據(jù)加載解析順序的完整代碼,感興趣的朋友跟隨小編一起看看吧
    2024-03-03
  • Vue+Element實現(xiàn)封裝抽屜彈框

    Vue+Element實現(xiàn)封裝抽屜彈框

    這篇文章主要為大家詳細介紹了如何利用Vue和Element實現(xiàn)簡單的抽屜彈框效果,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2023-06-06
  • Vue檢測屏幕變化來改變不同的charts樣式實例

    Vue檢測屏幕變化來改變不同的charts樣式實例

    這篇文章主要介紹了Vue檢測屏幕變化來改變不同的charts樣式實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10
  • vuecli項目構建SSR服務端渲染的實現(xiàn)

    vuecli項目構建SSR服務端渲染的實現(xiàn)

    這篇文章主要介紹了vuecli項目構建SSR服務端渲染的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-10-10
  • vue監(jiān)聽滾動事件實現(xiàn)滾動監(jiān)聽

    vue監(jiān)聽滾動事件實現(xiàn)滾動監(jiān)聽

    本文主要介紹了vue監(jiān)聽滾動事件實現(xiàn)滾動監(jiān)聽的相關資料。具有很好的參考價值。下面跟著小編一起來看下吧
    2017-04-04
  • Vue?props傳入function時的this指向問題解讀

    Vue?props傳入function時的this指向問題解讀

    這篇文章主要介紹了Vue?props傳入function時的this指向問題解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • Vue.js如何實現(xiàn)路由懶加載淺析

    Vue.js如何實現(xiàn)路由懶加載淺析

    Vue是可以自定義指令的,最近學習過程中遇見了一個需要懶加載的功能,發(fā)現(xiàn)網(wǎng)上這方面的資料較少,所以下面這篇文章主要給大家介紹了關于Vue.js如何實現(xiàn)路由懶加載的相關資料,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-08-08

最新評論