You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.7 KiB

2 years ago
<template>
<div v-if="!item.hidden">
<template v-if="hasOneShowingChild(item.children,item) && (!onlyOneChild.children||onlyOneChild.noShowingChildren)&&!item.alwaysShow">
2 years ago
<app-link v-if="onlyOneChild.meta" :to="resolvePath(onlyOneChild.path)">
<el-menu-item :index="resolvePath(onlyOneChild.path)" :class="{'submenu-title-noDropdown':!isNest}">
<item :icon="onlyOneChild.meta.icon||(item.meta&&item.meta.icon)" :title="generateTitle(onlyOneChild.meta.title)" />
2 years ago
</el-menu-item>
</app-link>
</template>
<el-submenu v-else ref="subMenu" :index="resolvePath(item.path)" popper-append-to-body>
2 years ago
<template slot="title">
<item v-if="item.meta" :icon="item.meta && item.meta.icon" :title="generateTitle(item.meta.title)" />
2 years ago
</template>
<sidebar-item
v-for="child in item.children"
:key="child.path"
:is-nest="true"
:item="child"
:base-path="resolvePath(child.path)"
class="nest-menu"
/>
</el-submenu>
</div>
</template>
<script>
import path from 'path'
import { generateTitle } from '@/utils/i18n'
import { isExternal } from '@/utils/validate'
import Item from './Item'
import AppLink from './Link'
import FixiOSBug from './FixiOSBug'
2 years ago
export default {
name: 'SidebarItem',
2 years ago
components: { Item, AppLink },
mixins: [FixiOSBug],
props: {
// route object
item: {
type: Object,
required: true
2 years ago
},
isNest: {
type: Boolean,
default: false
2 years ago
},
basePath: {
type: String,
default: ''
}
2 years ago
},
data() {
// To fix https://github.com/PanJiaChen/vue-admin-template/issues/237
// TODO: refactor with render function
this.onlyOneChild = null
return {}
2 years ago
},
methods: {
hasOneShowingChild(children = [], parent) {
const showingChildren = children.filter(item => {
2 years ago
if (item.hidden) {
return false
2 years ago
} else {
// Temp set(will be used if only has one showing child)
this.onlyOneChild = item
return true
2 years ago
}
})
2 years ago
// When there is only one child router, the child router is displayed by default
if (showingChildren.length === 1) {
return true
2 years ago
}
// Show parent if there are no child router to display
if (showingChildren.length === 0) {
this.onlyOneChild = { ... parent, path: '', noShowingChildren: true }
return true
2 years ago
}
return false
2 years ago
},
resolvePath(routePath) {
if (isExternal(routePath)) {
return routePath
2 years ago
}
if (isExternal(this.basePath)) {
return this.basePath
2 years ago
}
return path.resolve(this.basePath, routePath)
2 years ago
},
generateTitle
}
}
2 years ago
</script>