vue動(dòng)態(tài)設(shè)置路由權(quán)限的主要思路
之前看到網(wǎng)上有些動(dòng)態(tài)設(shè)置路由的,但是跟目前的項(xiàng)目不是很匹配,就自己動(dòng)手實(shí)現(xiàn)了一種。主要思路就是:
1.配置路由的時(shí)候綁定好id,可后端開(kāi)發(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è)置本地路由與后端傳來(lái)的路由的聯(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'){ // 從登錄過(guò)來(lái) 或者是回首頁(yè)
next(firstLink);
}else { // 刷新
if(navIds.indexOf(to.meta.id) == -1){ // 后端沒(méi)有返回該菜單
Message.error('菜單不存在或者沒(méi)有權(quán)限');
return;
}
next();
}
}
export const setGuard = (to={},from={},next=()=>{}) =>{ // 設(shè)置權(quán)限
if(navIds.length === 0){ // 還沒(méi)有獲取菜單數(shù)據(jù)
getNav(to,from,next);
}else { // 獲取到菜單數(shù)據(jù)
if(navIds.indexOf(to.meta.id) == -1){ // 后端沒(méi)有返回該菜單
Message.error('菜單不存在或者沒(méi)有權(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 { // 沒(méi)有登錄
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 history模式刷新頁(yè)面404問(wèn)題及解決
文章介紹了Vue單頁(yè)應(yīng)用中出現(xiàn)404錯(cuò)誤的原因,以及如何通過(guò)配置Nginx和使用Vue Router的hash模式來(lái)解決這個(gè)問(wèn)題,同時(shí),文章還簡(jiǎn)要解釋了單頁(yè)應(yīng)用的概念及其優(yōu)點(diǎn)和缺點(diǎn),并討論了Router的實(shí)現(xiàn)方式2024-12-12
vue打包上傳服務(wù)器加載提示錯(cuò)誤Loading chunk {n} failed
這篇文章主要為大家介紹了vue打包上傳服務(wù)器加載提示錯(cuò)誤Loading chunk {n} failed解決方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
解決vue ui報(bào)錯(cuò)Couldn‘t parse bundle asset“C:
這篇文章主要介紹了解決vue ui報(bào)錯(cuò)Couldn‘t parse bundle asset“C:\Users\Administrator\vue_project1\dist\js\about.js“. Analyzer問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04
利用vue寫(xiě)todolist單頁(yè)應(yīng)用
有很多關(guān)于vue的todolist小程序,這篇文章主要介紹了 用vue寫(xiě)todolist單頁(yè)應(yīng)用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
vue實(shí)現(xiàn)動(dòng)態(tài)路由的方法及路由原理解析
這篇文章主要介紹了路由原理及vue實(shí)現(xiàn)動(dòng)態(tài)路由,Vue Router 提供了豐富的 API,可以輕松地實(shí)現(xiàn)路由功能,并支持路由參數(shù)、查詢參數(shù)、命名路由、嵌套路由等功能,可以滿足不同應(yīng)用程序的需求,需要的朋友可以參考下2023-06-06
vue-router vuex-oidc動(dòng)態(tài)路由實(shí)例及功能詳解
這篇文章主要為大家介紹了vue-router vuex-oidc動(dòng)態(tài)路由實(shí)例及功能詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11
Vue前端如何實(shí)現(xiàn)與后端進(jìn)行數(shù)據(jù)交互
這篇文章主要介紹了Vue前端如何實(shí)現(xiàn)與后端進(jìn)行數(shù)據(jù)交互,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
詳解polyfills如何按需加載及場(chǎng)景示例詳解
這篇文章主要為大家介紹了詳解polyfills如何按需加載及場(chǎng)景示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02

