|
@@ -3,7 +3,7 @@ import { createRouter, RouterOptions, RouteRecordRaw, RouteLocationNormalized }
|
|
|
|
|
|
|
|
interface HistoryState {
|
|
interface HistoryState {
|
|
|
historyStacks: RouteLocationNormalized[]; // 已访问的路由列表
|
|
historyStacks: RouteLocationNormalized[]; // 已访问的路由列表
|
|
|
- excludeName: string[]; // 不缓存的组件名称
|
|
|
|
|
|
|
+ excludeViews: string[]; // 不缓存的页面
|
|
|
actionName: '' | 'push' | 'replace' | 'forward' | 'back'; // 当前路由动作
|
|
actionName: '' | 'push' | 'replace' | 'forward' | 'back'; // 当前路由动作
|
|
|
transitionName: '' | 'slide-right' | 'slide-left'; // 前进后退动画
|
|
transitionName: '' | 'slide-right' | 'slide-left'; // 前进后退动画
|
|
|
}
|
|
}
|
|
@@ -11,7 +11,7 @@ interface HistoryState {
|
|
|
export default new (class {
|
|
export default new (class {
|
|
|
private _state = ref<HistoryState>({
|
|
private _state = ref<HistoryState>({
|
|
|
historyStacks: [],
|
|
historyStacks: [],
|
|
|
- excludeName: [],
|
|
|
|
|
|
|
+ excludeViews: [],
|
|
|
actionName: '',
|
|
actionName: '',
|
|
|
transitionName: '',
|
|
transitionName: '',
|
|
|
})
|
|
})
|
|
@@ -77,8 +77,8 @@ export default new (class {
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
router.afterEach(() => {
|
|
router.afterEach(() => {
|
|
|
- const { excludeName } = toRefs(this._state.value);
|
|
|
|
|
- excludeName.value = [];
|
|
|
|
|
|
|
+ const { excludeViews } = toRefs(this._state.value);
|
|
|
|
|
+ excludeViews.value = [];
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
return router;
|
|
return router;
|
|
@@ -89,15 +89,15 @@ export default new (class {
|
|
|
* @param route
|
|
* @param route
|
|
|
*/
|
|
*/
|
|
|
private addHistory = (route: RouteLocationNormalized) => {
|
|
private addHistory = (route: RouteLocationNormalized) => {
|
|
|
- const { historyStacks, excludeName, actionName, transitionName } = toRefs(this._state.value);
|
|
|
|
|
|
|
+ const { historyStacks, excludeViews, actionName, transitionName } = toRefs(this._state.value);
|
|
|
|
|
|
|
|
// 如果是替换动作,必定是前进
|
|
// 如果是替换动作,必定是前进
|
|
|
if (actionName.value === 'replace') {
|
|
if (actionName.value === 'replace') {
|
|
|
const lastIndex = historyStacks.value.length - 1;
|
|
const lastIndex = historyStacks.value.length - 1;
|
|
|
- const lastPage = historyStacks.value[lastIndex];
|
|
|
|
|
|
|
+ const lastView = historyStacks.value[lastIndex];
|
|
|
|
|
|
|
|
- if (lastPage) {
|
|
|
|
|
- excludeName.value.push(lastPage.name as string);
|
|
|
|
|
|
|
+ if (lastView) {
|
|
|
|
|
+ excludeViews.value.push(lastView.name as string);
|
|
|
historyStacks.value[lastIndex] = route; // 更新最后一条记录
|
|
historyStacks.value[lastIndex] = route; // 更新最后一条记录
|
|
|
} else {
|
|
} else {
|
|
|
historyStacks.value.push(route);
|
|
historyStacks.value.push(route);
|
|
@@ -123,7 +123,7 @@ export default new (class {
|
|
|
const i = index + 1;
|
|
const i = index + 1;
|
|
|
const n = historyStacks.value.length - i;
|
|
const n = historyStacks.value.length - i;
|
|
|
|
|
|
|
|
- excludeName.value = historyStacks.value.map((e) => e.name).slice(-n) as string[]; // 返回数组最后位置开始的n个元素
|
|
|
|
|
|
|
+ excludeViews.value = historyStacks.value.map((e) => e.name).slice(-n) as string[]; // 返回数组最后位置开始的n个元素
|
|
|
historyStacks.value.splice(i, n); // 从i位置开始删除后面所有元素(包括i)
|
|
historyStacks.value.splice(i, n); // 从i位置开始删除后面所有元素(包括i)
|
|
|
}
|
|
}
|
|
|
transitionName.value = 'slide-right'; //后退动画
|
|
transitionName.value = 'slide-right'; //后退动画
|