| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- /**
- * 路由监听工具
- */
- /**
- * 监听路由变化
- * @param {Function} onChange 路由变化回调函数,参数为 (to, from)
- * @returns {void}
- */
- export function useRouteMonitor(onChange) {
- // 添加路由拦截器
- uni.addInterceptor('navigateTo', {
- invoke(e) {
- // 获取当前页面路由
- const pages = getCurrentPages()
- const from = pages[pages.length - 1]?.route || ''
- onChange && onChange(e.url, from)
- console.log('navigateTo', e.url, from)
- return e
- }
- })
- uni.addInterceptor('redirectTo', {
- invoke(e) {
- const pages = getCurrentPages()
- const from = pages[pages.length - 1]?.route || ''
- onChange && onChange(e.url, from)
- console.log('redirectTo', e.url, from)
- return e
- }
- })
- uni.addInterceptor('reLaunch', {
- invoke(e) {
- const pages = getCurrentPages()
- const from = pages[pages.length - 1]?.route || ''
- onChange && onChange(e.url, from)
- console.log('reLaunch', e.url, from)
- return e
- }
- })
- uni.addInterceptor('switchTab', {
- invoke(e) {
- const pages = getCurrentPages()
- const from = pages[pages.length - 1]?.route || ''
- onChange && onChange(e.url, from)
- console.log('switchTab', e.url, from)
- return e
- }
- })
- }
|