819527061@qq.com
2023-07-25 f0b2b04b1b43d4891c1808ad060b9959f9a1209c
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
<!--
 * @User: EricGU178
 * @Date: 2019-02-19 13:49:20
 * @LastEditors: EricGU178
 * @LastEditTime: 2019-02-22 10:48:52
 * @温馨提示: 代码千万行 注释第一行 命名不规范 同事^ ^
 * @菜单栏组件
 -->
<template>
  <div class="sidebar">
    <a-menu
      theme="dark"
      mode="inline"
      @openChange="onOpenChange"
      :inlineCollapsed="isColl"
      :openKeys="openkeys"
      :selectedKeys="[onRoutes]"
    >
      <template v-for="item in items">
        <template v-if="item.subs">
          <a-sub-menu :key="item.index">
            <span slot="title">
              <a-icon :type="item.icon" />
              <span>{{ item.title }}</span>
            </span>
            <a-menu-item
              v-for="sub in item.subs"
              :key="sub.index"
              v-on:click="titleClick(sub.index)"
            >
              <a-icon :type="sub.icon" />
              {{ sub.title }}
            </a-menu-item>
          </a-sub-menu>
        </template>
        <template v-else>
          <a-menu-item :key="item.index" v-on:click="titleClick(item.index)">
            <a-icon :type="item.icon" />
            <span>{{item.title}}</span>
          </a-menu-item>
        </template>
      </template>
    </a-menu>
  </div>
</template>
 
<script>
import bars from "../common/bars";
export default {
  data() {
    return {
      isColl: false,
      openkeys: [],
      items: this.$store.state.user.menus,
    };
  },
  methods: {
    // 跳转
    titleClick: function (res) {
      this.$router.push({ path: `/${res}` });
    },
    onOpenChange: function (res) {
      if (res[1]) {
        this.openkeys = [res[1]];
        return false;
      }
      if (res[0]) {
        this.openkeys = [res[0]];
        return false;
      }
      this.openkeys = [];
    },
  },
  computed: {
    // 计算当前路由的地址 改变 菜单中选中状态
    onRoutes() {
      let openKey = this.$route.path.replace("/", "");
      for (let i = 0; i < this.items.length; i++) {
        if (this.items[i].subs) {
          for (let j = 0; j < this.items[i].subs.length; j++) {
            if (openKey == this.items[i].subs[j].index) {
              this.openkeys = [this.items[i].index];
            }
          }
        }
      }
      return this.$route.path.replace("/", "");
    },
  },
  created() {
    // 通过bars.js通信
    bars.$on("isColl", (msg) => {
      this.isColl = msg;
    });
  },
};
</script>
 
<style scoped>
.sidebar {
  display: block;
  position: absolute;
  left: 0;
  top: 70px;
  bottom: 0;
  overflow-y: scroll;
}
.sidebar::-webkit-scrollbar {
  width: 0;
}
.ant-menu:not(.ant-menu-inline-collapsed) {
  width: 200px;
}
.sidebar > ul {
  height: 100%;
}
</style>