navigation.vue
3.02 KB
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
<template>
<div class="navigation-box">
<el-tabs
type="card"
v-model="activeIndex"
closable
@tab-click="tabClick"
@tab-remove="tabRemove"
>
<el-tab-pane
:key="index+'only'"
v-for="(item, index) in openTab"
:label="item.name"
:name="item.route"
></el-tab-pane>
</el-tabs>
</div>
</template>
<script>
export default {
data() {
return {
}
},
mounted() {
// 刷新时以当前路由做为tab加入tabs
// 当前路由不是首页时,添加首页以及另一页到store里,并设置激活状态
// 当当前路由是首页时,添加首页到store,并设置激活状态
if (this.$route.path !== "/" && this.$route.path !== "/panel") {
this.$store.dispatch("setTabs/add_tabs", { route: "/panel", name: "首页" })
this.$store.dispatch("setTabs/add_tabs", {
route: this.$route.path,
name: this.$route.name,
});
this.$store.dispatch("setTabs/set_active_index", this.$route.path)
} else {
this.$store.dispatch("setTabs/add_tabs", { route: "/panel", name: "首页" })
this.$store.dispatch("setTabs/set_active_index", "/panel")
}
},
computed: {
openTab() {
return this.$store.state.setTabs.openTab;
},
activeIndex: {
get() {
return this.$store.state.setTabs.activeIndex;
},
set(val) {
this.$store.dispatch("setTabs/set_active_index", val);
},
},
},
watch: {
$route(to, from) {
//判断路由是否已经打开
//已经打开的 ,将其置为active
//未打开的,将其放入队列里
let flag = false;
for (let item of this.openTab) {
if (item.name === to.name) {
this.$store.dispatch("setTabs/set_active_index", to.path);
flag = true;
break;
}
}
if (!flag) {
console.log("to.path", to.path);
this.$store.dispatch("setTabs/add_tabs", { route: to.path, name: to.name });
this.$store.dispatch("setTabs/set_active_index", to.path);
}
},
},
methods: {
//tab标签点击时,切换相应的路由
tabClick(tab) {
this.$router.push({ path: this.activeIndex });
},
//移除tab标签
tabRemove(targetName) {
//首页不删
if (targetName == "/" || targetName == "/panel") {
return;
}
this.$store.dispatch("setTabs/delete_tabs", targetName);
if (this.activeIndex === targetName) {
// 设置当前激活的路由
if (this.openTab && this.openTab.length >= 1) {
this.$store.dispatch(
"setTabs/set_active_index",
this.openTab[this.openTab.length - 1].route
);
this.$router.push({ path: this.activeIndex });
} else {
this.$router.push({ path: "/" });
}
}
},
},
};
</script>
<style lang="less">
.navigation-box {
.el-tabs__nav .el-tabs__item:nth-child(1) span {
display: none;
}
}
.el-tabs__nav .is-active{
background-color: #F1F4FC;
}
</style>