Explorar el Código

perf[Sidebar]: optimize logic

Pan hace 6 años
padre
commit
5bfadbc118

+ 9 - 8
src/views/layout/components/Sidebar/Item.vue

@@ -3,17 +3,18 @@ export default {
3
   name: 'MenuItem',
3
   name: 'MenuItem',
4
   functional: true,
4
   functional: true,
5
   props: {
5
   props: {
6
-    icon: {
7
-      type: String,
8
-      default: ''
9
-    },
10
-    title: {
11
-      type: String,
12
-      default: ''
6
+    meta: {
7
+      type: Object,
8
+      default: () => {
9
+        return {
10
+          title: '',
11
+          icon: ''
12
+        }
13
+      }
13
     }
14
     }
14
   },
15
   },
15
   render(h, context) {
16
   render(h, context) {
16
-    const { icon, title } = context.props
17
+    const { icon, title } = context.props.meta
17
     const vnodes = []
18
     const vnodes = []
18
 
19
 
19
     if (icon) {
20
     if (icon) {

+ 13 - 24
src/views/layout/components/Sidebar/SidebarItem.vue

@@ -1,33 +1,25 @@
1
 <template>
1
 <template>
2
-  <div v-if="!item.hidden&&item.children" class="menu-wrapper">
2
+  <div v-if="!item.hidden" class="menu-wrapper">
3
 
3
 
4
     <template v-if="hasOneShowingChild(item.children,item) && (!onlyOneChild.children||onlyOneChild.noShowingChildren)&&!item.alwaysShow">
4
     <template v-if="hasOneShowingChild(item.children,item) && (!onlyOneChild.children||onlyOneChild.noShowingChildren)&&!item.alwaysShow">
5
       <app-link :to="resolvePath(onlyOneChild.path)">
5
       <app-link :to="resolvePath(onlyOneChild.path)">
6
         <el-menu-item :index="resolvePath(onlyOneChild.path)" :class="{'submenu-title-noDropdown':!isNest}">
6
         <el-menu-item :index="resolvePath(onlyOneChild.path)" :class="{'submenu-title-noDropdown':!isNest}">
7
-          <item v-if="onlyOneChild.meta" :icon="onlyOneChild.meta.icon||item.meta.icon" :title="onlyOneChild.meta.title" />
7
+          <item :meta="Object.assign({},item.meta,onlyOneChild.meta)" />
8
         </el-menu-item>
8
         </el-menu-item>
9
       </app-link>
9
       </app-link>
10
     </template>
10
     </template>
11
 
11
 
12
-    <el-submenu v-else :index="resolvePath(item.path)">
12
+    <el-submenu v-else ref="subMenu" :index="resolvePath(item.path)">
13
       <template slot="title">
13
       <template slot="title">
14
-        <item v-if="item.meta" :icon="item.meta.icon" :title="item.meta.title" />
15
-      </template>
16
-
17
-      <template v-for="child in item.children" v-if="!child.hidden">
18
-        <sidebar-item
19
-          v-if="child.children&&child.children.length>0"
20
-          :is-nest="true"
21
-          :item="child"
22
-          :key="child.path"
23
-          :base-path="resolvePath(child.path)"
24
-          class="nest-menu" />
25
-        <app-link v-else :to="resolvePath(child.path)" :key="child.name">
26
-          <el-menu-item :index="resolvePath(child.path)">
27
-            <item v-if="child.meta" :icon="child.meta.icon" :title="child.meta.title" />
28
-          </el-menu-item>
29
-        </app-link>
14
+        <item :meta="item.meta" />
30
       </template>
15
       </template>
16
+      <sidebar-item
17
+        v-for="child in item.children"
18
+        :is-nest="true"
19
+        :item="child"
20
+        :key="child.path"
21
+        :base-path="resolvePath(child.path)"
22
+        class="nest-menu" />
31
     </el-submenu>
23
     </el-submenu>
32
 
24
 
33
   </div>
25
   </div>
@@ -64,7 +56,7 @@ export default {
64
     return {}
56
     return {}
65
   },
57
   },
66
   methods: {
58
   methods: {
67
-    hasOneShowingChild(children, parent) {
59
+    hasOneShowingChild(children = [], parent) {
68
       const showingChildren = children.filter(item => {
60
       const showingChildren = children.filter(item => {
69
         if (item.hidden) {
61
         if (item.hidden) {
70
           return false
62
           return false
@@ -89,13 +81,10 @@ export default {
89
       return false
81
       return false
90
     },
82
     },
91
     resolvePath(routePath) {
83
     resolvePath(routePath) {
92
-      if (this.isExternalLink(routePath)) {
84
+      if (isExternal(routePath)) {
93
         return routePath
85
         return routePath
94
       }
86
       }
95
       return path.resolve(this.basePath, routePath)
87
       return path.resolve(this.basePath, routePath)
96
-    },
97
-    isExternalLink(routePath) {
98
-      return isExternal(routePath)
99
     }
88
     }
100
   }
89
   }
101
 }
90
 }