index.vue
5.6 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
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
<template>
<el-tabs v-model="activeIndex"
v-if="openTab.length"
type="card"
:closable = "tabCloseable"
@tab-click='tabClick'
@tab-remove="tabRemove"
style="width: 100%">
<el-tab-pane
v-for="(item, index) in this.$store.state.openTab"
:key="item.name"
:label="item.name"
:name="item.route"
>
<section class="content-container">
<div class="grid-content bg-purple-light">
<!-- <el-col :span="24" class="breadcrumb-container">-->
<!-- <strong class="title">{{$route.name}}</strong>-->
<!-- <el-breadcrumb separator="/" class="breadcrumb-inner">-->
<!-- <el-breadcrumb-item v-for="item in $route.matched" :key="item.path">-->
<!-- {{ item.name }}-->
<!-- </el-breadcrumb-item>-->
<!-- </el-breadcrumb>-->
<!-- </el-col>-->
<el-col :span="24" class="content-wrapper">
<transition name="fade" mode="out-in">
<router-view></router-view>
</transition>
</el-col>
</div>
</section>
</el-tab-pane>
</el-tabs>
</template>
<script>
export default {
name: 'TabMenu',
data() {
return {
tabCloseable: true
}
},
methods: {
initTab(){
// 刷新时以当前路由做为tab加入tabs
// 当前路由不是首页时,添加首页以及另一页到store里,并设置激活状态
// 当当前路由是首页时,添加首页到store,并设置激活状态
if (this.$route.path !== '/' && this.$route.path !== '/main') {
// console.log('1');
this.$store.commit('add_tabs', {route: '/main' , name: '首页'});
this.$store.commit('add_tabs', {route: this.$route.path , name: this.$route.name });
this.$store.commit('set_active_index', this.$route.path);
} else {
// console.log('2');
this.$store.commit('add_tabs', {route: '/main', name: '首页'});
this.$store.commit('set_active_index', '/main');
this.$router.push('/main');
}
},
tabClick(tab){
// console.log("tab",tab);
// console.log('active',this.$store.state.activeIndex);
this.$router.push({path: this.$store.state.activeIndex});
},
tabRemove(targetName){
// console.log("tabRemove",targetName);
//首页不删
if(targetName == '/main'){
return
}
this.$store.commit('delete_tabs', targetName);
if (this.$store.state.activeIndex === targetName) {
// 设置当前激活的路由
if (this.$store.state.openTab && this.$store.state.openTab.length >=1) {
// console.log('=============',this.$store.state.openTab[this.$store.state.openTab.length-1].route)
this.$store.commit('set_active_index', this.$store.state.openTab[this.$store.state.openTab.length-1].route);
this.$router.push({path: this.$store.state.activeIndex});
this.tabCloseable = false;
} else {
this.$router.push({path: '/main'});
}
}
},
},
mounted() {
this.initTab();
},
watch:{
'$route'(to,from){
//判断路由是否已经打开
//已经打开的 ,将其置为active
//未打开的,将其放入队列里
let flag = false;
for(let item of this.$store.state.openTab){
// console.log("item.name",item.name)
// console.log("t0.name",to.name)
if(item.name === to.name){
// console.log('to.path',to.path);
this.$store.commit('set_active_index',to.path)
flag = true;
break;
}
}
if(!flag){
// console.log('to.path',to.path);
this.$store.commit('add_tabs', {route: to.path, name: to.name});
this.$store.commit('set_active_index', to.path);
}
}
},
computed:{
openTab:{
get: function () {
return this.$store.state.openTab;
},
set:function (value) {
this.$store.commit('set_tabs', {value});
// console.log("opebTab监视:value = "+ value);
}
},
activeIndex:{
get:function () {
return this.$store.state.activeIndex;
},
set:function (value) {
this.$store.commit('set_active_index', value);
}
}
}
}
</script>
<style>
</style>