|
|
@@ -9,15 +9,17 @@ interface HistoryStack {
|
|
|
}
|
|
|
|
|
|
interface HistoryState {
|
|
|
- history: HistoryStack[]; // 已访问的路由列表
|
|
|
+ historyStacks: HistoryStack[]; // 已访问的路由列表
|
|
|
historyIndex: number; // 当前历史索引位置
|
|
|
excludeViews: string[]; // 不缓存的页面
|
|
|
actionName: '' | 'push' | 'replace' | 'forward' | 'back'; // 当前路由动作
|
|
|
}
|
|
|
|
|
|
export default new (class {
|
|
|
+ private storageKey = 'pc@' + window.location.hostname;
|
|
|
+
|
|
|
private _state = ref<HistoryState>({
|
|
|
- history: [],
|
|
|
+ historyStacks: [],
|
|
|
historyIndex: 0,
|
|
|
excludeViews: [],
|
|
|
actionName: '',
|
|
|
@@ -27,7 +29,7 @@ export default new (class {
|
|
|
state;
|
|
|
|
|
|
constructor() {
|
|
|
- const state = sessionStorage.getItem('historyState');
|
|
|
+ const state = sessionStorage.getItem(this.storageKey);
|
|
|
if (state) {
|
|
|
const data: HistoryState = JSON.parse(state);
|
|
|
this._state.value = data;
|
|
|
@@ -43,7 +45,7 @@ export default new (class {
|
|
|
create = (options: RouterOptions) => {
|
|
|
const router = createRouter(options);
|
|
|
const { push, replace, go, forward, back } = router;
|
|
|
- const { actionName } = toRefs(this._state.value);
|
|
|
+ const { actionName, excludeViews } = toRefs(this._state.value);
|
|
|
|
|
|
// 添加
|
|
|
router.push = (to: RouteRecordRaw) => {
|
|
|
@@ -81,16 +83,17 @@ export default new (class {
|
|
|
}
|
|
|
|
|
|
router.beforeResolve((to) => {
|
|
|
- this.addHistory({
|
|
|
- name: to.name || to.path,
|
|
|
- title: to.meta.title as string ?? '标签页',
|
|
|
- fullPath: to.fullPath,
|
|
|
- redirected: !!to.redirectedFrom,
|
|
|
- });
|
|
|
+ if (to.meta.keepAlive) {
|
|
|
+ this.addHistory({
|
|
|
+ name: to.name || to.path,
|
|
|
+ title: to.meta.title as string ?? '标签页',
|
|
|
+ fullPath: to.fullPath,
|
|
|
+ redirected: !!to.redirectedFrom,
|
|
|
+ });
|
|
|
+ }
|
|
|
})
|
|
|
|
|
|
router.afterEach(() => {
|
|
|
- const { excludeViews } = toRefs(this._state.value);
|
|
|
excludeViews.value = [];
|
|
|
})
|
|
|
|
|
|
@@ -102,34 +105,34 @@ export default new (class {
|
|
|
* @param route
|
|
|
*/
|
|
|
private addHistory = (route: HistoryStack) => {
|
|
|
- const { history, historyIndex, excludeViews, actionName } = toRefs(this._state.value);
|
|
|
- const index = history.value.findIndex((e) => e.name === route.name);
|
|
|
+ const { historyStacks, historyIndex, excludeViews, actionName } = toRefs(this._state.value);
|
|
|
+ const index = historyStacks.value.findIndex((e) => e.name === route.name);
|
|
|
|
|
|
if (index > -1) {
|
|
|
- history.value[index] = route;
|
|
|
+ historyStacks.value[index] = route;
|
|
|
historyIndex.value = index;
|
|
|
} else {
|
|
|
if (actionName.value === 'replace') {
|
|
|
- const lastIndex = history.value.length - 1;
|
|
|
- const lastView = history.value[lastIndex];
|
|
|
+ const lastIndex = historyStacks.value.length - 1;
|
|
|
+ const lastView = historyStacks.value[lastIndex];
|
|
|
|
|
|
if (lastView) {
|
|
|
excludeViews.value.push(lastView.name as string);
|
|
|
- history.value[lastIndex] = route; // 更新最后一条记录
|
|
|
+ historyStacks.value[lastIndex] = route; // 更新最后一条记录
|
|
|
} else {
|
|
|
- history.value.push(route);
|
|
|
+ historyStacks.value.push(route);
|
|
|
}
|
|
|
} else {
|
|
|
// 忽略重定向的页面
|
|
|
if (!route.redirected) {
|
|
|
- history.value.push(route);
|
|
|
- historyIndex.value = history.value.length - 1;
|
|
|
+ historyStacks.value.push(route);
|
|
|
+ historyIndex.value = historyStacks.value.length - 1;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
actionName.value = '';
|
|
|
- sessionStorage.setItem('historyState', JSON.stringify(this._state.value));
|
|
|
+ sessionStorage.setItem(this.storageKey, JSON.stringify(this._state.value));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -138,13 +141,13 @@ export default new (class {
|
|
|
*/
|
|
|
removeHistory = (routeName?: string) => {
|
|
|
if (routeName) {
|
|
|
- const { history, excludeViews } = toRefs(this._state.value);
|
|
|
- const index = history.value.findIndex((e) => e.name === routeName);
|
|
|
+ const { historyStacks, excludeViews } = toRefs(this._state.value);
|
|
|
+ const index = historyStacks.value.findIndex((e) => e.name === routeName);
|
|
|
excludeViews.value.push(routeName);
|
|
|
|
|
|
if (index > -1) {
|
|
|
- history.value.splice(index, 1);
|
|
|
- sessionStorage.setItem('historyState', JSON.stringify(this._state.value));
|
|
|
+ historyStacks.value.splice(index, 1);
|
|
|
+ sessionStorage.setItem(this.storageKey, JSON.stringify(this._state.value));
|
|
|
}
|
|
|
}
|
|
|
}
|