duhuizhe
2023-10-16 3aa55dd3f62cee2c1c4c0aa74e1570acf83f8927
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<template>
  <div v-if="!item.hidden">
    <template
      v-if="
        hasOneShowingChild(item.childList, item) &&
        (!onlyOneChild.childList || onlyOneChild.noShowingChildren) &&
        !item.alwaysShow
      "
    >
      <router-link v-if="onlyOneChild" :to="onlyOneChild.component">
        <el-menu-item :index="resolvePath(onlyOneChild.component)" :class="{ 'submenu-title-noDropdown': !isNest }">
<!--          <span v-if="activeMenu===resolvePath(onlyOneChild.component)" class="cur-icon"><span class="dot"/></span>
          <span v-else class="cur-empty"/>-->
          <item :icon="onlyOneChild.extra || (item.meta && item.extra)" :title="onlyOneChild.name"/>
        </el-menu-item>
      </router-link>
    </template>
    <el-submenu v-else ref="subMenu" :index="resolvePath(item.component)" popper-append-to-body>
      <template slot="title">
        <item v-if="item" :icon="item && item.icon" :title="item.name"/>
      </template>
      <sidebar-item
        v-for="(childs, i) in item.childList"
        :key="i"
        :is-nest="true"
        :item="childs"
        :base-path="resolvePath(childs.component)"
        class="nest-menu"
      />
    </el-submenu>
  </div>
</template>
 
<script>
import path from 'path';
import {isExternal} from '@/utils/validate';
import Item from './Item';
import AppLink from './Link';
import FixiOSBug from './FixiOSBug';
 
export default {
  name: 'SidebarItem',
  components: {Item, AppLink},
  mixins: [FixiOSBug],
  props: {
    // route object
    item: {
      type: Object,
      required: true,
    },
    isNest: {
      type: Boolean,
      default: false,
    },
    basePath: {
      type: String,
      default: '',
    },
  },
  data() {
    // To fix https://github.com/PanJiaChen/vue-admin-template/issues/237
    // TODO: refactor with render function
    this.onlyOneChild = null;
    return {};
  },
  // mounted(){
  //   console.log('kkkk',this.item)
  // },
  mounted() {
  },
  computed: {
    activeMenu() {
      const route = this.$route
      const {meta, path} = route
      // if set path, the sidebar will highlight the path you set
      if (meta.activeMenu) {
        return meta.activeMenu
      }
      return path
    }
  },
  methods: {
    hasOneShowingChild(childList = [], parent) {
      const showingChildren = childList.filter((item) => {
        if (item.hidden) {
          return false;
        } else {
          // Temp set(will be used if only has one showing childList)
          this.onlyOneChild = item;
          return true;
        }
      });
 
      // When there is only one childList router, the childList router is displayed by default
      if (showingChildren.length === 1) {
        return true;
      }
 
      // Show parent if there are no childList router to display
      if (showingChildren.length === 0) {
        this.onlyOneChild = {...parent, path: '', noShowingChildren: true};
        return true;
      }
 
      return false;
    },
    resolvePath(routePath) {
      // console.log(this.item);
      // console.log("............" + routePath);
      if (isExternal(routePath)) {
        return routePath;
      }
      if (isExternal(this.basePath)) {
        return this.basePath;
      }
      // console.log(this.basePath, routePath)
      return path.resolve(this.basePath, routePath);
    },
  },
};
</script>
<style lang="scss" scoped>
> > > .el-submenu__title {
  height: 35px;
  line-height: 35px;
}
 
> > > .is-opened .el-submenu__title {
  background-color: #ECF2FF;
  color: #0d997c;
 
  i {
    color: #0d997c
  }
}
/*
> > > .el-menu-item {
  padding-left: 5px !important;
}*/
 
> > > .el-menu-item:hover, .el-menu-item:focus {
  background: #fff;
  color: #0d997c;
}
 
.cur-icon {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 5px;
  border: 1px solid #0d997c;
  position: relative;
 
  .dot {
    position: absolute;
    top: 2px;
    left: 2px;
    display: inline-block;
    width: 4px;
    height: 4px;
    border-radius: 2px;
    background-color: #0d997c;
  }
}
 
.cur-empty {
  display: inline-block;
  width: 10px;
  height: 10px;
}
</style>