-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
39 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { patchRouter } from '../utils' | ||
import { turnApp } from './routerHandle' | ||
|
||
// 重写路由跳转: 实现路由拦截 | ||
export const rewriteRouter = () => { | ||
|
||
window.history.pushState = patchRouter(window.history.pushState, 'micro_push') | ||
window.history.replaceState = patchRouter(window.history.replaceState, 'micro_replace') | ||
|
||
// add:event bind | ||
window.addEventListener('micro_push', turnApp) | ||
window.addEventListener('micro_replace', turnApp) | ||
|
||
// 监听返回事件 | ||
window.onpopstate = function () { | ||
turnApp() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// 每次路由切换打印事件 | ||
export const turnApp = ()=>{ | ||
console.log('路由切换了'); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// 给当前路由跳转打补丁 | ||
export const patchRouter = (globalEvent, eventName)=>{ | ||
return function(){ | ||
const e = new Event(eventName) | ||
globalEvent.apply(this, arguments) | ||
window.dispatchEvent(e) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters