// 重写window的路由跳转
export const rewriteRouter = () => {
window.history.pushState = patchRouter(window.history.pushState, 'micro_push')
window.history.replaceState = patchRouter(window.history.replaceState, 'micro_replace')
window.addEventListener('micro_push', turnApp)
window.addEventListener('micro_replace', turnApp)
// 监听返回事件
window.onpopstate = async function () {
await turnApp()
}
}
// 给当前的路由跳转打补丁
export const patchRouter = (globalEvent, eventName) => {
return function () {
const e = new Event(eventName)
globalEvent.apply(this, arguments) // 执行window的路由跳转 bus lupe
window.dispatchEvent(e) // 执行自定义事件:micro_push、micro_replace
}
}
07-09
1546
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)