Tabs.vue
2.76 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
<template>
<div class="tabs-box flex-row flex-items-center">
<div class="tabs-item paas-flex-cc" v-for="item in tabsList" :key="item.id" :class="item.path === currentPage ? 'tabs-item-hover' : 'null'">
<span>
<span class="cursor-pointer fs-14 tabs-item-text" @click="handleClick(item)">{{ item.title }}</span>
<i class="el-icon-close tabs-remove-icon cursor-pointer" @click="removeTab(item)"></i>
</span>
</div>
</div>
</template>
<script>
import { mapState } from 'vuex'
export default {
name: 'Tabs',
computed: {
...mapState({
menuList: state => state.app.menuList,
currentPage: state => state.app.currentPage,
tabsList: state => state.tabs.tabsList
})
},
methods: {
// 获取元素数组下标
getArrayIndex(arr, id) {
for (let index = 0; index < arr.length; index++) {
const item = arr[index]
if (item.id === id) {
return index
}
}
return -1
},
// 跳转tabs标签
handleClick(item) {
if (this.currentPage.id === item.id) return false
// 设置左侧菜单数据
if (item.path !== '/home') {
const menu = this.menuList.filter(element => {
return element.id === item.id.split('-')[0]
})
this.$store.commit('app/UPDATE_SUB_MENU', menu[0].menuList)
this.$router.push({ path: item.path })
} else {
this.$store.commit('app/UPDATE_SUB_MENU', true)
this.$router.push({ path: item.path })
}
},
// 删除tabs标签
removeTab(item) {
let tabsListLength = this.tabsList.length - 1
// 获取当前要删除的tabs的位置
let indexOf = this.getArrayIndex(this.tabsList, item.id)
if (tabsListLength === indexOf) {
// 删除最后一个
this.$store.dispatch('tabs/REMOVE_LAST_TAB', item)
} else {
// 删除除了第一个跟最后一个
this.$store.dispatch('tabs/REMOVE_ANY_TAB', {
indexOf,
tabsItem: item
})
}
}
}
}
</script>
<style lang="scss" scoped>
.tabs-box {
height: 0.32rem;
padding: 0.1rem;
.tabs-item {
height: 100%;
padding: 0 0.12rem;
margin-right: 0.1rem;
background-color: #fff;
border-radius: 0.04rem;
color: #333;
font-size: 0.14rem;
line-height: 0.32rem;
white-space: nowrap;
.tabs-item-text {
&:hover {
color: #23b7e5;
}
}
.tabs-remove-icon {
margin-left: 0.02rem;
padding: 0.02rem;
font-size: 0.04px;
cursor: pointer;
}
.tabs-remove-icon:hover {
border-radius: 50%;
color: #fff;
background-color: #23b7e5;
}
}
.tabs-item:last-child {
border-right: none !important;
}
}
.tabs-item-hover {
color: #23b7e5 !important;
}
</style>