chore: 整理
All checks were successful
CI / cache-and-install (push) Successful in 1m7s

This commit is contained in:
2024-08-13 23:02:51 +08:00
parent 9ecbb42312
commit fcff296d4e
13 changed files with 127 additions and 362 deletions

View File

@ -2,72 +2,12 @@
import { router } from '@/router';
definePage({
alias: '/'
alias: '/',
});
const testButtons = [
{
text: 'Page1 -> Page2',
onClick: async () => {
console.clear();
console.debug('⚙️ 预期结果:', ['Page1', 'Page2']);
await router.push({ name: 'Page1' });
await router.push({ name: 'Page2' });
}
},
{
text: 'A -> (B) -> C',
onClick: async () => {
console.clear();
console.debug('⚙️ 预期结果: ', ['Page1', 'Page3']);
await router.push({ name: 'Page1' });
await router.push({ name: 'Page2' });
await router.push({ name: 'Page3', replace: true });
}
},
{
text: 'A -> B -> C -> go(-2)',
onClick: async () => {
console.clear();
console.debug('⚙️ 预期结果: ', ['/', '/Page1📍', '/Page2', '/Page3']);
await router.push({ name: 'Page1' });
await router.push({ name: 'Page2' });
await router.push({ name: 'Page3' });
router.go(-2);
}
},
{
text: 'A -> B -> C -> go(-1) -> (D)',
onClick: async () => {
console.clear();
console.debug('⚙️ 预期结果: ', ['/', 'Page1', 'Page4📍', 'Page3']);
await router.push({ name: 'Page1' });
await router.push({ name: 'Page2' });
await router.push({ name: 'Page3' });
router.go(-1);
function popStateListener() {
console.debug('back执行完成, 替换"Page2"为"Page4"');
router.replace({ name: 'Page4' });
window.removeEventListener('popstate', popStateListener);
}
window.addEventListener('popstate', popStateListener);
}
}
];
</script>
<template>
<div class="flex flex-col gap-2 items-start">
<div class="flex gap-2">
<t-button @click="$router.push({ name: 'Page1' })">ToPage1</t-button>
<t-button @click="$router.push({ name: 'Page2' })">ToPage2</t-button>
<t-button @click="$router.push({ name: 'Page3' })">ToPage3</t-button>
</div>
<template v-for="btn in testButtons" :key="btn.text">
<t-button theme="primary" @click="btn.onClick">{{ btn.text }}</t-button>
</template>
<div>
<h1>Index Page</h1>
</div>
</template>

View File

@ -1,10 +0,0 @@
<template>
<div>
<t-button @click="$router.back()">back()</t-button>
</div>
<div class="h-2" />
<div class="flex gap-2">
<t-button @click="$router.push({ name: 'Page2' })">ToPage2</t-button>
<t-button @click="$router.replace({ name: 'Page2' })">ReplaceToPage2</t-button>
</div>
</template>

View File

@ -1,31 +0,0 @@
<template>
<!-- <VanButton @click="backToHome">Back to Home</VanButton> -->
<div class="flex gap-2">
<t-button @click="$router.go(-1)">go(-1)</t-button>
<t-button @click="$router.go(-2)">go(-2)</t-button>
</div>
</template>
<script setup lang="ts">
import { router } from '@/router';
function backToHome() {
console.log('router.options.history.state.back :>> ', router.options.history.state.back);
const backToName = 'IndexPage';
console.log('backToName :>> ', backToName);
let backCount = 0;
/* for (let i = router.stack.currentStackIndex - 1; i >= 0; i--) {
if (router.stack.list[i].name === backToName) {
backCount = router.stack.currentStackIndex - i;
break;
}
} */
console.log('backCount :>> ', backCount);
}
onMounted(() => {
// console.log('router.currentRoute.value.from :>> ', router.currentRoute.value.from);
// backToHome()
});
</script>

View File

@ -1 +0,0 @@
<template>page-3</template>

View File

@ -1 +0,0 @@
<template>page-4</template>

View File

@ -1,16 +0,0 @@
import { createLogGuard, createProgressGuard } from '@yanhao98/vue-router-helper';
import type { Router } from 'vue-router';
import { createStackGuard } from './stack-guard';
// Don't change the order of creation
export function setupRouterGuard(router: Router) {
createProgressGuard(router);
createLogGuard(router);
createStackGuard(router);
router.onError((error) => {
console.debug('🚨 [router error]: ', error);
});
}

View File

@ -1,133 +0,0 @@
import { START_LOCATION } from 'vue-router';
import type { RouteLocationNormalized, Router } from 'vue-router';
export function createStackGuard(router: Router) {
// const stack = router.stack = { /* list: [] as RouteLocationNormalized[], */ currentStackIndex: 0 }
let startPosition = -1;
let curPosition = -1;
// if ($__DEV__) Object.assign(window, { stack })
const _routerHistory = router.options.history;
/* window.addEventListener('beforeunload', function (event) {
console.debug("🚥", '[onbeforeunload]', "event :>> ", event);
const confirmationMessage = "\\o/";
(event || window.event).returnValue = confirmationMessage; //Gecko + IE
return confirmationMessage; //Webkit, Safari, Chrome etc.
}) */
_routerHistory.listen((to, from, info) => {
console.debug('🚥 listen', ':');
console.debug('🚥 listen', 'from :>> ', from);
console.debug('🚥 listen', 'to :>> ', to); // TODO: 在afterEach那里核对
console.debug('🚥 listen', 'info :>> ', info);
// lastNavigationInfo.lastInfo = info;
});
// #############################
// 重写 router.history 方法
// #############################
() => {
const routerHistoryPush = _routerHistory.push;
_routerHistory.push = function (...args) {
console.debug('🚥 [router]', 'push: args :>> ', args);
return routerHistoryPush.call(this, ...args);
};
const routerHistoryReplace = _routerHistory.replace;
_routerHistory.replace = function (...args) {
console.debug('🚥 [router]', 'replace: args :>> ', args);
// lastNavigationInfo.replace = true;
return routerHistoryReplace.call(this, ...args);
};
const routerHistoryGo = _routerHistory.go;
_routerHistory.go = function (...args) {
console.debug('🚥 [router]', 'go: args :>> ', args);
return routerHistoryGo.call(this, ...args);
};
} /* () ;*/;
// #############################
// 重写 window.history 方法
// #############################
() => {
const browserHistoryBack = window.history.back;
window.history.back = function (...args) {
console.debug('🌐 [Browser]', 'history.back: args :>> ', args);
return browserHistoryBack.call(this, ...args);
};
const browserHistoryForward = window.history.forward;
window.history.forward = function (...args) {
console.debug('🌐 [Browser]', 'history.forward: args :>> ', args);
return browserHistoryForward.call(this, ...args);
};
const browserHistoryGo = window.history.go;
window.history.go = function (...args) {
console.debug('🌐 [Browser]', 'history.go: args :>> ', args);
return browserHistoryGo.call(this, ...args);
};
const browserHistoryPushState = window.history.pushState;
window.history.pushState = function (...args) {
console.debug('🌐 [Browser]', 'history.pushState: args :>> ', args);
return browserHistoryPushState.call(this, ...args);
};
const browserHistoryReplaceState = window.history.replaceState;
window.history.replaceState = function (...args) {
console.debug('🌐 [Browser]', 'history.replaceState: args :>> ', args);
return browserHistoryReplaceState.call(this, ...args);
};
}; /* () */
/* router.beforeEach(async (to) => {
if (to.name === 'Page3') {
return { name: 'Page2' }
}
}) */
router.afterEach(async (to, from, failure) => {
if (failure) return;
if (from === START_LOCATION) {
startPosition = history.state.position;
curPosition = startPosition - 1;
}
const newPostion = history.state.position;
// console.log('history.state.position :>> ', history.state.position);
console.log('startPosition :>> ', startPosition);
console.log('newPostion :>> ', newPostion);
const delta = newPostion - curPosition;
const stackIdx = newPostion - startPosition;
if (newPostion > curPosition) {
console.log('👉🏻');
} else if (newPostion < curPosition) {
console.log('👈🏻');
// stack.splice(delta)
} else {
console.log('🔄');
}
Object.assign(to, { locationHref: window.location.href });
stack[stackIdx] = to as StackItem;
console.log('delta :>> ', delta);
curPosition = newPostion;
console.log(
'stack :>> ',
stack.map((item, index) => (stackIdx === index ? `🔵 ${item.name?.toString}` : item.name)),
stack
);
console.log(`%c${'-'.repeat(80)}`, 'color: #409EFF;');
});
}
declare type StackItem = RouteLocationNormalized & { locationHref: string };
const stack: StackItem[] = [];
Object.assign(window, { stack });

View File

@ -1,17 +1,28 @@
import { createRouter, createWebHistory } from 'vue-router';
import { createProgressGuard, createLogGuard, createStackGuard } from '@yanhao98/vue-router-helper';
import { createRouter, createWebHistory, type Router } from 'vue-router';
import { routes, handleHotUpdate } from 'vue-router/auto-routes';
import { setupRouterGuard } from './guard';
export const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes,
strict: true
strict: true,
});
if ($__DEV__) Object.assign(window, { router });
setupRouterGuard(router);
// Don't change the order of creation
function setupRouterGuard(router: Router) {
createProgressGuard(router);
createLogGuard(router);
Object.assign(window, { stack: createStackGuard(router) });
router.onError((error) => {
console.debug('🚨 [router error]: ', error);
});
}
if (import.meta.hot) {
handleHotUpdate(router);
}