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

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

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

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

1.配置路由的時(shí)候綁定好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'){ // 從登錄過來 或者是回首頁(yè)
 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動(dòng)態(tài)設(shè)置路由權(quán)限的文章就介紹到這了,更多相關(guān)vue動(dòng)態(tài)設(shè)置路由權(quán)限內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue?cli3?項(xiàng)目中如何使用axios發(fā)送post請(qǐng)求

    vue?cli3?項(xiàng)目中如何使用axios發(fā)送post請(qǐng)求

    這篇文章主要介紹了vue?cli3?項(xiàng)目中如何使用axios發(fā)送post請(qǐng)求,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • vue中使用axios請(qǐng)求post接口發(fā)送兩次

    vue中使用axios請(qǐng)求post接口發(fā)送兩次

    這篇文章主要為大家介紹了vue中使用axios請(qǐng)求post接口,請(qǐng)求會(huì)發(fā)送兩次原因解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-11-11
  • 解決vue create 創(chuàng)建項(xiàng)目只有兩個(gè)文件問題

    解決vue create 創(chuàng)建項(xiàng)目只有兩個(gè)文件問題

    這篇文章主要介紹了解決vue create 創(chuàng)建項(xiàng)目只有兩個(gè)文件問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • Vue一個(gè)案例引發(fā)的遞歸組件的使用詳解

    Vue一個(gè)案例引發(fā)的遞歸組件的使用詳解

    這篇文章主要介紹了Vue一個(gè)案例引發(fā)的遞歸組件的使用,本文主要給大家展示如何在項(xiàng)目中使用遞歸組件,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-11-11
  • 手把手教你搭建vue3.0項(xiàng)目架構(gòu)

    手把手教你搭建vue3.0項(xiàng)目架構(gòu)

    這篇文章手把手教你搭建vue3.0項(xiàng)目架構(gòu),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2021-11-11
  • vue全局實(shí)現(xiàn)數(shù)字千位分隔符格式

    vue全局實(shí)現(xiàn)數(shù)字千位分隔符格式

    這篇文章主要為大家詳細(xì)介紹了vue全局實(shí)現(xiàn)數(shù)字千位分隔符格式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • Vue路由對(duì)象屬性 .meta $route.matched詳解

    Vue路由對(duì)象屬性 .meta $route.matched詳解

    今天小編就為大家分享一篇Vue路由對(duì)象屬性 .meta $route.matched詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • vue內(nèi)置指令詳解

    vue內(nèi)置指令詳解

    指令是Vue模板中最常用的一項(xiàng)功能,它帶有前綴v-,主要職責(zé)是當(dāng)其表達(dá)式的值改變時(shí),相應(yīng)的將某些行為應(yīng)用在 DOM 上。這篇文章主要介紹了vue內(nèi)置指令詳解,需要的朋友可以參考下
    2018-04-04
  • vue3的watch和watchEffect你了解嗎

    vue3的watch和watchEffect你了解嗎

    這篇文章主要為大家詳細(xì)介紹了vue的watch和watchEffect,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • 詳解如何更好的使用module vuex

    詳解如何更好的使用module vuex

    這篇文章主要介紹了詳解如何更好的使用module vuex,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-03-03

最新評(píng)論