SidebarItem.vue 2.56 KB
Newer Older
Pan's avatar
Pan committed
1
<template>
2
  <div v-if="!item.hidden&&item.children" class="menu-wrapper">
Pan's avatar
Pan committed
3

4
    <template v-if="hasOneShowingChild(item.children) && !onlyOneChild.children&&!item.alwaysShow">
5
      <a :href="onlyOneChild.path" target="_blank" @click="clickLink(onlyOneChild.path,$event)">
6 7 8 9 10
        <el-menu-item :index="resolvePath(onlyOneChild.path)" :class="{'submenu-title-noDropdown':!isNest}">
          <item v-if="onlyOneChild.meta" :icon="onlyOneChild.meta.icon" :title="onlyOneChild.meta.title" />
        </el-menu-item>
      </a>
    </template>
Pan's avatar
Pan committed
11

12 13
    <el-submenu v-else :index="item.name||item.path">
      <template slot="title">
14
        <item v-if="item.meta" :icon="item.meta.icon" :title="item.meta.title" />
15
      </template>
Pan's avatar
Pan committed
16

17 18 19 20 21 22 23 24
      <template v-for="child in item.children" v-if="!child.hidden">
        <sidebar-item
          v-if="child.children&&child.children.length>0"
          :is-nest="true"
          :item="child"
          :key="child.path"
          :base-path="resolvePath(child.path)"
          class="nest-menu"/>
Pan's avatar
Pan committed
25

26
        <a v-else :href="child.path" :key="child.name" target="_blank" @click="clickLink(child.path,$event)">
27
          <el-menu-item :index="resolvePath(child.path)">
28
            <item v-if="child.meta" :icon="child.meta.icon" :title="child.meta.title" />
29
          </el-menu-item>
30
        </a>
31 32
      </template>
    </el-submenu>
Pan's avatar
Pan committed
33

Pan's avatar
Pan committed
34
  </div>
Pan's avatar
Pan committed
35 36 37
</template>

<script>
38
import path from 'path'
39 40
import { validateURL } from '@/utils/validate'
import Item from './Item'
41

Pan's avatar
Pan committed
42 43
export default {
  name: 'SidebarItem',
44
  components: { Item },
Pan's avatar
Pan committed
45
  props: {
46 47 48 49
    // route配置json
    item: {
      type: Object,
      required: true
50 51 52 53
    },
    isNest: {
      type: Boolean,
      default: false
54 55 56 57
    },
    basePath: {
      type: String,
      default: ''
Pan's avatar
Pan committed
58
    }
59
  },
60 61 62 63 64
  data() {
    return {
      onlyOneChild: null
    }
  },
65
  methods: {
66
    hasOneShowingChild(children) {
67
      const showingChildren = children.filter(item => {
68 69 70 71 72 73 74
        if (item.hidden) {
          return false
        } else {
          // temp set(will be used if only has one showing child )
          this.onlyOneChild = item
          return true
        }
75 76 77 78 79
      })
      if (showingChildren.length === 1) {
        return true
      }
      return false
80
    },
81 82 83 84 85
    resolvePath(routePath) {
      return path.resolve(this.basePath, routePath)
    },
    isExternalLink(routePath) {
      return validateURL(routePath)
86 87 88 89 90 91 92
    },
    clickLink(routePath, e) {
      if (!this.isExternalLink(routePath)) {
        e.preventDefault()
        const path = this.resolvePath(routePath)
        this.$router.push(path)
      }
93
    }
Pan's avatar
Pan committed
94 95
  }
}
Pan's avatar
Pan committed
96
</script>