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

vue動態(tài)設(shè)置路由權(quán)限的主要思路

 更新時間:2021年01月13日 14:15:23   作者:愛寫代碼的漁夫  
這篇文章主要給大家介紹了關(guān)于vue動態(tài)設(shè)置路由權(quán)限的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

之前看到網(wǎng)上有些動態(tài)設(shè)置路由的,但是跟目前的項(xiàng)目不是很匹配,就自己動手實(shí)現(xiàn)了一種。主要思路就是:

1.配置路由的時候綁定好id,可后端開發(fā)完成后,與后端同步id就行,這id唯一不變,根據(jù)此id可找到路由地址及icon。

const routerArr = [
 {
 path: '',
 name: '',
 component: () => import( /* webpackChunkName: "strategiesMaintain" */ '@/components/Layout/Index'),
 meta: {
 requireAuth: true,
 id: 1,
 icon: 'iconzhanghuguanli',
 title: '路由1'
 },
 children: [{ 
 path: '/verificationLog',
 name: 'VerificationLog',
 component: () => import( /* webpackChunkName: "verificationLog" */ '@/views/auditManage/verificationLog'),
 meta: {
 requireAuth: true,
 id: 101,
 icon: 'icon-disanfangyanzhengrizhi',
 title: '路由11'
 }
 }, {
 path: '/systemLog',
 name: 'SystemLog',
 component: () => import( /* webpackChunkName: "systemLog" */ '@/views/auditManage/systemLog'),
 meta: {
 requireAuth: true,
 id: 102,
 icon: 'icon-xitongcaozuorizhi',
 title: '路由12'
 }
 }]
 }
];

export default routerArr;

2.設(shè)置本地路由與后端傳來的路由的聯(lián)系,主要是根據(jù)id綁定路由地址及iconClass

import routerModules from "@/router/modules";
import {http} from '@/utils/http'
import store from '@/store';
import { Message } from 'element-ui'

const formateResData = (val) =>{ // 格式化路由數(shù)據(jù)
 const obj = {};
 const fn = (arr)=>{
  for(let i = 0,item;item = arr[i++];){
  obj[item['meta']['id']] = {
   path: item['path'],
   iconClass: item['meta']['icon']
  };
  if(item.children && item.children.length > 0){
   fn(item.children);
  }
  }
 }
 fn(val);
 return obj;
};

const MAPOBJ = formateResData(routerModules);
const dealWithData = (navData) => { // 處理菜單數(shù)據(jù)
 let firstLink = "";
 const navIdArr = [];
 const fn = (arr) => {
  for (let i = 0,item;item = arr[i++];) {
  item['iconClass'] = MAPOBJ[item.id].iconClass;
  item['linkAction'] = MAPOBJ[item.id].path;
  navIdArr.push(item.id);
  if (!firstLink && !item.subMenu) { // 設(shè)置默認(rèn)跳轉(zhuǎn)
   firstLink = item['linkAction'];
  }
  if (item.subMenu && item.subMenu.length > 0) {
   fn(item.subMenu);
  }
  }
 }
 fn(navData);
 return {navData,navIdArr,firstLink};
};

let navIds = [];

const getNav = async (to={},from={},next=()=>{})=>{ // 獲取導(dǎo)航數(shù)據(jù)
 const {code,data} = await http("/menu/list", {}, "GET"); // 獲取菜單數(shù)據(jù)
 // const data = require("@/mock/api/menuData"); // 使用mock數(shù)據(jù)
 const {navData,navIdArr,firstLink} = dealWithData(data);
 store.commit('setNavData', navData);
 navIds = navIdArr;
 if(to.fullPath == '/index'){ // 從登錄過來 或者是回首頁
 next(firstLink);
 }else { // 刷新
 if(navIds.indexOf(to.meta.id) == -1){ // 后端沒有返回該菜單
  Message.error('菜單不存在或者沒有權(quán)限');
  return;
 }
 next();
 }
}

export const setGuard = (to={},from={},next=()=>{}) =>{ // 設(shè)置權(quán)限
 if(navIds.length === 0){ // 還沒有獲取菜單數(shù)據(jù)
 getNav(to,from,next);
 }else { // 獲取到菜單數(shù)據(jù)
 if(navIds.indexOf(to.meta.id) == -1){ // 后端沒有返回該菜單
  Message.error('菜單不存在或者沒有權(quán)限');
  return;
 }
 next();
 }
}

3.在mainjs中引入配置

router.beforeEach((to, from, next) => {
 let token = wlhStorage.get("authorization");
 if (to.path == "/login") {
 storage.clear();// 清空緩存
 next();
 } else {
 if (to.meta.requireAuth && token) { // 登陸
  setGuard(to,from,next);
 } else { // 沒有登錄
  next("/login");
 }
 }
})

總結(jié)

到此這篇關(guān)于vue動態(tài)設(shè)置路由權(quán)限的文章就介紹到這了,更多相關(guān)vue動態(tài)設(shè)置路由權(quán)限內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Vue history模式刷新頁面404問題及解決

    Vue history模式刷新頁面404問題及解決

    文章介紹了Vue單頁應(yīng)用中出現(xiàn)404錯誤的原因,以及如何通過配置Nginx和使用Vue Router的hash模式來解決這個問題,同時,文章還簡要解釋了單頁應(yīng)用的概念及其優(yōu)點(diǎn)和缺點(diǎn),并討論了Router的實(shí)現(xiàn)方式
    2024-12-12
  • vue打包上傳服務(wù)器加載提示錯誤Loading chunk {n} failed

    vue打包上傳服務(wù)器加載提示錯誤Loading chunk {n} failed

    這篇文章主要為大家介紹了vue打包上傳服務(wù)器加載提示錯誤Loading chunk {n} failed解決方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • 解決vue ui報(bào)錯Couldn‘t parse bundle asset“C:\Users\Administrator\vue_project1\dist\js\about.js“. Analyzer

    解決vue ui報(bào)錯Couldn‘t parse bundle asset“C:

    這篇文章主要介紹了解決vue ui報(bào)錯Couldn‘t parse bundle asset“C:\Users\Administrator\vue_project1\dist\js\about.js“. Analyzer問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • 利用vue寫todolist單頁應(yīng)用

    利用vue寫todolist單頁應(yīng)用

    有很多關(guān)于vue的todolist小程序,這篇文章主要介紹了 用vue寫todolist單頁應(yīng)用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • vue實(shí)現(xiàn)動態(tài)路由的方法及路由原理解析

    vue實(shí)現(xiàn)動態(tài)路由的方法及路由原理解析

    這篇文章主要介紹了路由原理及vue實(shí)現(xiàn)動態(tài)路由,Vue Router 提供了豐富的 API,可以輕松地實(shí)現(xiàn)路由功能,并支持路由參數(shù)、查詢參數(shù)、命名路由、嵌套路由等功能,可以滿足不同應(yīng)用程序的需求,需要的朋友可以參考下
    2023-06-06
  • Vue具名插槽+作用域插槽的混合使用方法

    Vue具名插槽+作用域插槽的混合使用方法

    這篇文章主要介紹了Vue具名插槽+作用域插槽的混合使用,這里只簡單的介紹?具名插槽+作用域插槽?混合在一起使用的方法,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-09-09
  • Vue2.0中集成UEditor富文本編輯器的方法

    Vue2.0中集成UEditor富文本編輯器的方法

    本文給大家詳細(xì)講述了Vue2.0中集成UEditor富文本編輯器的方法以及相關(guān)注意事項(xiàng)做了講述,有興趣的朋友學(xué)習(xí)下。
    2018-03-03
  • vue-router vuex-oidc動態(tài)路由實(shí)例及功能詳解

    vue-router vuex-oidc動態(tài)路由實(shí)例及功能詳解

    這篇文章主要為大家介紹了vue-router vuex-oidc動態(tài)路由實(shí)例及功能詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-11-11
  • Vue前端如何實(shí)現(xiàn)與后端進(jìn)行數(shù)據(jù)交互

    Vue前端如何實(shí)現(xiàn)與后端進(jìn)行數(shù)據(jù)交互

    這篇文章主要介紹了Vue前端如何實(shí)現(xiàn)與后端進(jìn)行數(shù)據(jù)交互,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • 詳解polyfills如何按需加載及場景示例詳解

    詳解polyfills如何按需加載及場景示例詳解

    這篇文章主要為大家介紹了詳解polyfills如何按需加載及場景示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02

最新評論

大宁县| 长岭县| 克拉玛依市| 南丰县| 江口县| 荆州市| 嵩明县| 孝昌县| 金川县| 建瓯市| 娱乐| 大渡口区| 商城县| 大竹县| 阜宁县| 思南县| 永吉县| 绵阳市| 综艺| 咸宁市| 乌什县| 葵青区| 永泰县| 安岳县| 晋州市| 铜山县| 黄平县| 赤城县| 德清县| 浏阳市| 彭水| 蕉岭县| 清河县| 通榆县| 德清县| 洞头县| 洛宁县| 天气| 商都县| 湖口县| 黄浦区|