long
2021-04-08 c2b28e50d4abc170a55090850f4a6cdd4b06d9e7
提交 | 用户 | age
2a61f6 1 <template>
L 2   <div v-if="!item.hidden">
3     <template v-if="hasOneShowingChild(item.children,item) && (!onlyOneChild.children||onlyOneChild.noShowingChildren)&&!item.alwaysShow">
4       <app-link v-if="onlyOneChild.meta" :to="resolvePath(onlyOneChild.path)">
5         <el-menu-item :index="resolvePath(onlyOneChild.path)" :class="{'submenu-title-noDropdown':!isNest}">
6           <item :icon="onlyOneChild.meta.icon||(item.meta&&item.meta.icon)" :title="onlyOneChild.meta.title" />
7         </el-menu-item>
8       </app-link>
9     </template>
10
11     <el-submenu v-else ref="subMenu" :index="resolvePath(item.path)" popper-append-to-body>
12       <template slot="title">
13         <item v-if="item.meta" :icon="item.meta && item.meta.icon" :title="item.meta.title" />
14       </template>
15       <sidebar-item
16         v-for="child in item.children"
17         :key="child.path"
18         :is-nest="true"
19         :item="child"
20         :base-path="resolvePath(child.path)"
21         class="nest-menu"
22       />
23     </el-submenu>
24   </div>
25 </template>
26
27 <script>
28 import path from 'path'
29 import { isExternal } from '@/utils/validate'
30 import Item from './Item'
31 import AppLink from './Link'
32 import FixiOSBug from './FixiOSBug'
33
34 export default {
35   name: 'SidebarItem',
36   components: { Item, AppLink },
37   mixins: [FixiOSBug],
38   props: {
39     // route object
40     item: {
41       type: Object,
42       required: true
43     },
44     isNest: {
45       type: Boolean,
46       default: false
47     },
48     basePath: {
49       type: String,
50       default: ''
51     }
52   },
53   data() {
54     // To fix https://github.com/PanJiaChen/vue-admin-template/issues/237
55     // TODO: refactor with render function
56     this.onlyOneChild = null
57     return {}
58   },
59   methods: {
60     hasOneShowingChild(children = [], parent) {
61       const showingChildren = children.filter(item => {
62         if (item.hidden) {
63           return false
64         } else {
65           // Temp set(will be used if only has one showing child)
66           this.onlyOneChild = item
67           return true
68         }
69       })
70
71       // When there is only one child router, the child router is displayed by default
72       if (showingChildren.length === 1) {
73         return true
74       }
75
76       // Show parent if there are no child router to display
77       if (showingChildren.length === 0) {
78         this.onlyOneChild = { ... parent, path: '', noShowingChildren: true }
79         return true
80       }
81
82       return false
83     },
84     resolvePath(routePath) {
85       if (isExternal(routePath)) {
86         return routePath
87       }
88       if (isExternal(this.basePath)) {
89         return this.basePath
90       }
91       return path.resolve(this.basePath, routePath)
92     }
93   }
94 }
95 </script>