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
| <style lang="less">
| @import "./styles/menu.less";
| </style>
|
| <template>
| <div
| :style="{ background: bgColor }"
| :class="`shrinkable-menu ${theme}-menu`"
| >
| <slot name="top"></slot>
| <sidebar-menu
| v-show="!shrink"
| :theme="theme"
| :menu-list="menuList"
| :open-names="openNames"
| @on-change="handleChange"
| ></sidebar-menu>
| <sidebar-menu-shrink
| v-show="shrink"
| :theme="theme"
| :menu-list="menuList"
| :open-names="openNames"
| @on-change="handleChange"
| ></sidebar-menu-shrink>
| </div>
| </template>
|
| <script>
| import sidebarMenu from "./components/sidebarMenu.vue";
| import sidebarMenuShrink from "./components/sidebarMenuShrink.vue";
| export default {
| name: "shrinkableMenu",
| components: {
| sidebarMenu,
| sidebarMenuShrink,
| },
| props: {
| shrink: {
| type: Boolean,
| default: false,
| },
| menuList: {
| type: Array,
| required: true,
| },
| theme: {
| type: String,
| default: "darkblue",
| },
| beforePush: {
| type: Function,
| },
| },
| data() {
| return {
| openNames: [],
| };
| },
| computed: {
| bgColor() {
| if (this.theme == "darkblue") {
| return "#17233d";
| } else if (this.theme == "dark") {
| return "#515a6e";
| } else if (this.theme == "black") {
| return "#1f1f1f";
| }
| return "#fff";
| },
| },
| methods: {
| handleChange(name) {
| let willpush = true;
| if (this.beforePush !== undefined) {
| if (!this.beforePush(name)) {
| willpush = false;
| }
| }
| if (name == this.$route.name) {
| willpush = false;
| }
| if (willpush) {
| this.$router.push({
| name: name,
| });
| }
| this.$emit("on-change", name);
| },
| },
| watch: {
| // 监听路由变化
| $route(to, from) {
| this.openNames = [this.$route.matched[0].name];
| },
| },
| mounted() {
| this.openNames = [this.$route.matched[0].name];
| },
| };
| </script>
|
|