1
2
3
4
import type { TabContentProps } from './tab.data';
import type { TabItem } from '/@/store/modules/tab';
import type { AppRouteRecordRaw } from '/@/router/types';
nebv
authored
5 years ago
5
6
7
8
9
10
import {
defineComponent,
watch,
computed,
// ref,
unref,
vben
authored
5 years ago
11
// onMounted,
vben
authored
5 years ago
12
toRaw,
nebv
authored
5 years ago
13
} from 'vue';
14
15
16
17
18
19
20
21
22
23
24
25
import { Tabs } from 'ant-design-vue';
import TabContent from './TabContent';
import { useGo } from '/@/hooks/web/usePage';
import { TabContentEnum } from './tab.data';
import { useRouter } from 'vue-router';
import { tabStore } from '/@/store/modules/tab';
import { closeTab } from './useTabDropdown';
import router from '/@/router';
vben
authored
5 years ago
26
import { useTabs } from '/@/hooks/web/useTabs';
vben
authored
5 years ago
27
// import { PageEnum } from '/@/enums/pageEnum';
vben
authored
5 years ago
28
nebv
authored
5 years ago
29
import './index.less';
vben
authored
5 years ago
30
import { userStore } from '/@/store/modules/user';
31
32
33
34
35
36
export default defineComponent({
name: 'MultiTabs',
setup() {
let isAddAffix = false;
const go = useGo();
const { currentRoute } = useRouter();
vben
authored
5 years ago
37
38
39
40
41
42
43
44
45
46
47
const {
// addTab,
activeKeyRef,
} = useTabs();
// onMounted(() => {
// const route = unref(currentRoute);
// addTab(unref(currentRoute).path as PageEnum, false, {
// query: route.query,
// params: route.params,
// });
// });
vben
authored
5 years ago
48
49
// 当前激活tab
nebv
authored
5 years ago
50
51
// const activeKeyRef = ref<string>('');
52
53
54
55
56
// 当前tab列表
const getTabsState = computed(() => {
return tabStore.getTabsState;
});
nebv
authored
5 years ago
57
58
59
60
61
if (!isAddAffix) {
addAffixTabs();
isAddAffix = true;
}
62
63
watch(
() => unref(currentRoute).path,
vben
authored
5 years ago
64
65
66
67
68
() => {
if (!userStore.getTokenState) return;
const { path: rPath, fullPath } = unref(currentRoute);
if (activeKeyRef.value !== (fullPath || rPath)) {
activeKeyRef.value = fullPath || rPath;
69
}
vben
authored
5 years ago
70
71
// 监听路由的话虽然可以,但是路由切换的时间会造成卡顿现象?
// 使用useTab的addTab的话,当用户手动调转,需要自行调用addTab
vben
authored
5 years ago
72
73
74
75
76
77
78
79
80
81
tabStore.commitAddTab((unref(currentRoute) as unknown) as AppRouteRecordRaw);
// const { affix } = currentRoute.value.meta || {};
// if (affix) return;
// const hasInTab = tabStore.getTabsState.some(
// (item) => item.fullPath === currentRoute.value.fullPath
// );
// if (!hasInTab) {
// tabStore.commitAddTab((unref(currentRoute) as unknown) as AppRouteRecordRaw);
// }
82
83
},
{
vben
authored
5 years ago
84
// flush: 'post',
85
86
87
immediate: true,
}
);
nebv
authored
5 years ago
88
89
90
91
92
93
94
95
96
/**
* @description: 过滤所有固定路由
*/
function filterAffixTabs(routes: AppRouteRecordRaw[]) {
const tabs: TabItem[] = [];
routes &&
routes.forEach((route) => {
if (route.meta && route.meta.affix) {
vben
authored
5 years ago
97
tabs.push(toRaw(route) as TabItem);
98
99
100
101
}
});
return tabs;
}
nebv
authored
5 years ago
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
* @description: 设置固定tabs
*/
function addAffixTabs(): void {
const affixTabs = filterAffixTabs((router.getRoutes() as unknown) as AppRouteRecordRaw[]);
for (const tab of affixTabs) {
tabStore.commitAddTab(tab);
}
}
// tab切换
function handleChange(activeKey: any) {
activeKeyRef.value = activeKey;
go(activeKey, false);
}
nebv
authored
5 years ago
118
119
120
121
// 关闭当前ab
function handleEdit(targetKey: string) {
// 新增操作隐藏,目前只使用删除操作
vben
authored
5 years ago
122
123
124
const index = unref(getTabsState).findIndex(
(item) => (item.fullPath || item.path) === targetKey
);
125
126
127
128
129
130
131
132
133
134
135
index !== -1 && closeTab(unref(getTabsState)[index]);
}
function renderQuick() {
const tabContentProps: TabContentProps = {
tabItem: (currentRoute as unknown) as AppRouteRecordRaw,
type: TabContentEnum.EXTRA_TYPE,
trigger: ['click', 'contextmenu'],
};
return (
<span>
vben
authored
5 years ago
136
<TabContent {...(tabContentProps as any)} />
137
138
139
140
141
</span>
);
}
function renderTabs() {
return unref(getTabsState).map((item: TabItem) => {
vben
authored
5 years ago
142
143
const key = item.query ? item.fullPath : item.path;
144
return (
vben
authored
5 years ago
145
<Tabs.TabPane key={key} closable={!(item && item.meta && item.meta.affix)}>
146
147
148
149
150
151
152
153
154
155
156
157
158
159
{{
tab: () => <TabContent tabItem={item} />,
}}
</Tabs.TabPane>
);
});
}
return () => {
return (
<div class="multiple-tabs">
<Tabs
type="editable-card"
size="small"
vben
authored
5 years ago
160
animated={false}
161
hideAdd={true}
vben
authored
5 years ago
162
tabBarGutter={4}
163
164
165
166
167
168
169
170
171
172
173
174
175
176
activeKey={unref(activeKeyRef)}
onChange={handleChange}
onEdit={handleEdit}
>
{{
default: () => renderTabs(),
tabBarExtraContent: () => renderQuick(),
}}
</Tabs>
</div>
);
};
},
});