Ver Fonte

Merge remote-tracking branch 'origin/master'

xgb há 1 semana atrás
pai
commit
7fbead5f4e

+ 30 - 13
src/layout/AdminLayout.vue

@@ -251,25 +251,42 @@ export default {
     activeRoutes(key) {
       const routes = []
       const normalizedKey = this.normPath(key)
-      const childrenMenus = this.buildChildrenMenus()
-      childrenMenus.forEach(item => {
-        if (this.normPath(item.parentPath) === normalizedKey) {
-          routes.push(item)
-        }
-      })
-      if (routes.length === 0) {
-        const router = this.topbarRouters.find(r => !r.hidden && this.normPath(r.path) === normalizedKey)
-        if (router && router.children) {
-          router.children.forEach(child => {
-            if (!child.hidden) routes.push(child)
-          })
-        }
+      const topRouter = this.topbarRouters.find(r => !r.hidden && this.normPath(r.path) === normalizedKey)
+      if (topRouter && topRouter.children && topRouter.children.length) {
+        topRouter.children.forEach(child => {
+          if (!child.hidden) {
+            routes.push(this.prefixSidebarRouteTree(child, normalizedKey))
+          }
+        })
+      } else {
+        const childrenMenus = this.buildChildrenMenus()
+        childrenMenus.forEach(item => {
+          if (this.normPath(item.parentPath) === normalizedKey) {
+            routes.push(item)
+          }
+        })
       }
       if (routes.length > 0) {
         this.$store.commit('SET_SIDEBAR_ROUTERS', routes)
       }
       return routes
     },
+    /** 为侧栏嵌套菜单补全相对 path,避免子菜单链接跳到 /operLog 等错误路径 */
+    prefixSidebarRouteTree(route, parentPath) {
+      const cloned = { ...route }
+      const seg = (cloned.path || '').replace(/^\//, '')
+      if (seg && !this.ishttp(cloned.path) && !cloned.path.startsWith('/')) {
+        cloned.path = this.normPath(parentPath) + '/' + seg
+      } else if (cloned.path && !cloned.path.startsWith('/') && !this.ishttp(cloned.path)) {
+        cloned.path = '/' + cloned.path
+      }
+      if (cloned.children && cloned.children.length) {
+        cloned.children = cloned.children.map(child =>
+          this.prefixSidebarRouteTree(child, cloned.path)
+        )
+      }
+      return cloned
+    },
     getFirstRoutePath(routes) {
       if (!routes || !routes.length) return null
       for (let i = 0; i < routes.length; i++) {

+ 1 - 1
src/layout/components/Sidebar/SidebarItem.vue

@@ -17,7 +17,7 @@
         :key="child.path"
         :is-nest="true"
         :item="child"
-        :base-path="resolvePath(child.path)"
+        :base-path="resolvePath(item.path)"
         class="nest-menu"
       />
     </el-submenu>

+ 2 - 1
src/store/modules/permission.js

@@ -124,7 +124,8 @@ function filterChildren(childrenMap, lastRouter = false) {
   var children = []
   childrenMap.forEach((el, index) => {
     if (el.children && el.children.length) {
-      if (el.component === 'ParentView') {
+      // ParentView 或空 component 的分组菜单都需要展平子路由
+      if (el.component === 'ParentView' || !el.component) {
         el.children.forEach(c => {
           c.path = el.path + '/' + c.path
           if (c.children && c.children.length) {