AdaKing88
2023-08-23 9cad48db6c56c3e2796a9d6da881817ef13b6eca
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
<template>
  <h1 class="logo">奥瑞金 MESS 管理平台</h1>
  <el-menu
    :collapse="collapse"
    :default-active="currentPath"
    background-color="#344a5f"
    text-color="#fff"
    active-text-color="#ffffff"
    router
  >
    <template v-for="item in routers" :key="item.path">
      <template v-if="!item.hidden">
        <!-- 一级菜单 -->
        <template v-if="hasOnlyChild(item.children)">
          <el-menu-item :index="item.children[0].path">
            <!-- <img class="menu-icon" :src="item.meta.icon" alt="" /> -->
            <template #title>{{
              item.children[0].meta && item.children[0].meta.title
            }}</template>
          </el-menu-item>
        </template>
        <!-- 子级菜单 -->
        <template v-else>
          <el-sub-menu
            v-if="item.children && item.children.length > 0"
            :index="item.path"
          >
            <template #title>
              <span>{{ item.meta && item.meta.title }}</span>
            </template>
            <template v-for="child in item.children" :key="child.path">
              <el-menu-item v-if="!child.hidden" :index="child.path">{{
                child.meta && child.meta.title
              }}</el-menu-item>
            </template>
          </el-sub-menu>
        </template>
      </template>
    </template>
  </el-menu>
</template>
 
<script>
import { useRouter, useRoute } from "vue-router";
import { reactive, computed, toRefs } from "vue";
import { useStore } from "vuex";
export default {
  name: "Aside",
  components: {},
  props: {},
  setup() {
    const { options } = useRouter();
    const { path } = useRoute();
    const store = useStore();
    const routers = options.routes;
    console.log(routers);
    /**
     * 数据
     */
    const data = reactive({
      logo: computed(() => {
        return store.state.app.collapse
          ? require("@/assets/images/logo-min.png")
          : require("@/assets/images/logo.png");
      }),
      collapse: computed(() => store.state.app.collapse),
    });
 
    /**
     * 判断是否只有一个子级菜单
     */
    const hasOnlyChild = (children) => {
      if (!children) {
        return false;
      }
      // 存储路由
      if (!children) {
        return false;
      }
      const childRouter = children.filter((item) => {
        return item.hidden ? false : true;
      });
 
      // 只有一个子级路由
      if (childRouter.length === 1) {
        return true;
      }
      // 否则
      return false;
    };
    /**
     * 获取当前路由路径
     */
    const currentPath = computed(() => path);
    // console.log(currentPath);
    return {
      ...toRefs(data),
      routers,
      hasOnlyChild,
      currentPath,
    };
  },
};
</script>
<style lang="scss" scoped>
.logo {
  padding: 20px 0;
  border-bottom: 1px solid #2d4153;
  color: #fff;
  font-size: 24px;
  font-weight: 600;
  text-align: center;
}
.menu-icon {
  width: 18px;
  height: 18px;
  display: inline-block;
  margin: 0 5px 0 0;
}
</style>