Преглед на файлове

feat(router): 为H5环境添加hashchange路由拦截

添加对H5环境的路由拦截支持,通过监听hashchange事件实现页面跳转拦截
feige996 преди 9 месеца
родител
ревизия
447e8e4626
променени са 1 файла, в които са добавени 10 реда и са изтрити 0 реда
  1. 10 0
      src/router/interceptor.ts

+ 10 - 0
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
   },
 }