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,可后端開發(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)求,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04vue中使用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è)文件問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02vue全局實(shí)現(xiàn)數(shù)字千位分隔符格式
這篇文章主要為大家詳細(xì)介紹了vue全局實(shí)現(xiàn)數(shù)字千位分隔符格式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10Vue路由對(duì)象屬性 .meta $route.matched詳解
今天小編就為大家分享一篇Vue路由對(duì)象屬性 .meta $route.matched詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11