From 0d86c19b122011dc528c624597b0952660d69aba Mon Sep 17 00:00:00 2001
From: loulijun2021 <1694218219@qq.com>
Date: 星期日, 26 六月 2022 09:32:53 +0800
Subject: [PATCH] 1.优化动态路由
---
src/store/modules/permission.js | 181 ++++++++++----------------------------------
1 files changed, 43 insertions(+), 138 deletions(-)
diff --git a/src/store/modules/permission.js b/src/store/modules/permission.js
index b6cba09..2b74277 100644
--- a/src/store/modules/permission.js
+++ b/src/store/modules/permission.js
@@ -1,4 +1,4 @@
-import { commonRoutes } from '@/router'
+import { asyncRoutes, commonRoutes } from '@/router'
import Layout from '@/layout'
// 鍒ゆ柇鏄惁鏈夋潈闄�
@@ -14,51 +14,49 @@
* 鎶婂悗鍙拌繑鍥炶彍鍗曠粍瑁呮垚routes瑕佹眰鐨勬牸寮�
* @param {*} routes
*/
-export function getAsyncRoutes(routes) {
- const res = []
+export function getAsyncRoutes(routes2, menu) {
+ const menuCode = []// 灏嗗悗绔痗ode鍙栧嚭瀛樻垚鏁扮粍褰㈠紡
+ menu.forEach(item => {
+ menuCode.push(item.code)
+ if (item.children && item.children.length > 0) {
+ item.children.forEach(it => {
+ menuCode.push(it.code)
+ })
+ }
+ })
+
+ const routes = routes2
+ const newRoutes = []
routes.forEach(item => {
- const newItem = {}
- if (item.component) {
- if (item.component === 'Layout') {
- newItem.component = Layout
- newItem.path = item.path
- if (item.redirect) {
- newItem.redirect = item.redirect
+ if (menuCode.includes(item.code) && item.children && item.children.length > 0) {
+ console.log(item)
+ const children = []
+ let flag = false// 鍒ゆ柇鏄惁鏈塩hildren瀛樿繘鍘�
+ item.children.forEach(it => {
+ if (menuCode.includes(it.code)) {
+ children.push(it)
+ flag = true
}
- if (item.hidden === '0') {
- newItem.hidden = false
- }
- if (item.hidden === '1') {
- newItem.hidden = true
- }
- if (item.meta) {
- newItem.meta = { title: item.meta.title, icon: item.meta.icon, affix: item.meta.affix === '1' }
- }
- } else {
- newItem.path = item.path
- if (item.name) {
- newItem.name = item.name
- }
- if (item.hidden === '0') {
- newItem.hidden = false
- }
- if (item.hidden === '1') {
- newItem.hidden = true
- }
- newItem.component = (resolve) => require(['@/views' + `${item.component}`], resolve)
- if (item.meta) {
- newItem.meta = { title: item.meta.title, icon: item.meta.icon, affix: item.meta.affix === '1' }
- }
+ })
+ if (flag) {
+ newRoutes.push({
+ alwaysShow: item.alwaysShow,
+ code: item.code,
+ name: item.name,
+ path: item.path,
+ redirect: item.redirect,
+ component: item.component,
+ children: children,
+ meta: item.meta
+ })
}
}
-
- if (item.children && item.children.length) {
- newItem.children = getAsyncRoutes(item.children)
- }
- res.push(newItem)
})
- return res
+
+ newRoutes.push({ path: '*', redirect: '/404', hidden: true })
+
+ return newRoutes
}
// 杩囨护鍑烘湁鏉冮檺鐨勮矾鐢�
@@ -89,105 +87,12 @@
}
const actions = {
- generateRoutes({ commit }, info) {
+ generateRoutes({ commit }, menu) {
return new Promise(resolve => {
- // const accessedRoutes
-
- // getMenuList().then(routes => {
- // const asyncRoutes = getAsyncRoutes(routes.data) // 瀵硅矾鐢辨牸寮忚繘琛屽鐞�
- // commit('SET_ROUTES', asyncRoutes)
- // resolve(asyncRoutes)
- // })
-
- // '0'浠h〃false,'1'浠h〃true
- const routes = [
- {
- path: '/',
- component: 'Layout',
- redirect: '/index',
- children: [{
- path: 'index',
- name: 'index',
- component: '/xtsy/index',
- meta: { title: '绯荤粺棣栭〉', icon: 'el-icon-house', affix: '1' }
- }]
- },
- {
- path: '/redirect',
- component: 'Layout',
- hidden: '1',
- children: [
- {
- path: '/redirect/:path(.*)',
- component: '/redirect/index'
- }
- ]
- },
- {
- path: '/jcsz',
- component: 'Layout',
- redirect: '/jcsz/zzjg',
- name: 'jcsz',
- meta: { title: '鍩虹璁剧疆', icon: 'el-icon-setting' },
- children: [
- {
- path: 'zzjg',
- name: 'Table',
- component: '/jcsz/zzjg',
- meta: { title: '缁勭粐鏋舵瀯', icon: '' }
- },
- {
- path: 'yhqd',
- name: 'yhqd',
- component: '/jcsz/yhqd',
- meta: { title: '鐢ㄦ埛娓呭崟', icon: '' }
- },
- {
- path: 'jsqd',
- name: 'jsqd',
- component: '/jcsz/jsqd',
- meta: { title: '瑙掕壊娓呭崟', icon: '' }
- },
- {
- path: 'wldw',
- name: 'wldw',
- component: '/jcsz/wldw',
- meta: { title: '寰�鏉ュ崟浣�', icon: '' }
- }
- ]
- },
- {
- path: '/zzmx',
- component: 'Layout',
- redirect: '/zzmx/gylx',
- name: 'zzmx',
- meta: { title: '鍒堕�犳ā鍨�', icon: 'el-icon-s-help' },
- children: [
- {
- path: 'gylx',
- name: 'gylx',
- component: '/zzmx/gylx',
- meta: { title: '鍏泭璺嚎', icon: '' }
- },
- {
- path: 'jpgj',
- name: 'jpgj',
- component: '/zzmx/jpgj',
- meta: { title: '鑺傛媿宸ヤ环', icon: '' }
- },
- {
- path: 'test',
- name: 'test',
- component: '/zzmx/test',
- meta: { title: '娴嬭瘯椤甸潰', icon: '' }
- }
- ]
- }
- ]
- const asyncRoutes = getAsyncRoutes(routes)
- asyncRoutes.push({ path: '*', redirect: '/404', hidden: true })
- commit('SET_ROUTES', asyncRoutes)
- resolve(asyncRoutes)
+ const newRoutes = getAsyncRoutes(asyncRoutes, menu)
+ console.log(newRoutes, 2)
+ commit('SET_ROUTES', newRoutes)
+ resolve(newRoutes)
})
}
}
--
Gitblit v1.9.3