From 447e8e4626921f293f0f8d848ef94850b44f91ef Mon Sep 17 00:00:00 2001 From: feige996 <1020102647@qq.com> Date: Wed, 6 Aug 2025 20:25:31 +0800 Subject: [PATCH] =?UTF-8?q?feat(router):=20=E4=B8=BAH5=E7=8E=AF=E5=A2=83?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0hashchange=E8=B7=AF=E7=94=B1=E6=8B=A6?= =?UTF-8?q?=E6=88=AA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加对H5环境的路由拦截支持,通过监听hashchange事件实现页面跳转拦截 --- src/router/interceptor.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/router/interceptor.ts b/src/router/interceptor.ts index 5fa96b9..f52a968 100644 --- a/src/router/interceptor.ts +++ b/src/router/interceptor.ts @@ -63,5 +63,15 @@ export const routeInterceptor = { uni.addInterceptor('reLaunch', navigateToInterceptor) uni.addInterceptor('redirectTo', navigateToInterceptor) uni.addInterceptor('switchTab', navigateToInterceptor) + + // #ifdef H5 + // 一个粗糙的实现方式,不满意可以自行修改:https://github.com/unibest-tech/unibest/issues/192 + // H5环境路由拦截,监听hashchange事件 + window.addEventListener('hashchange', () => { + // 获取当前路径 + const currentPath = `/${window.location.hash.split('#/')[1]?.split('?')[0]}` + navigateToInterceptor.invoke({ url: currentPath }) + }) + // #endif }, }