Skip to content

Commit

Permalink
feat(client): add onAfterPageLoad hook in router (#4126)
Browse files Browse the repository at this point in the history
  • Loading branch information
lejunyang authored Sep 1, 2024
1 parent 77beb44 commit 315c220
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/en/reference/runtime-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ interface Router {
* updated). Return `false` to cancel the navigation.
*/
onBeforePageLoad?: (to: string) => Awaitable<void | boolean>
/**
* Called after the page component is loaded (before the page component is updated).
*/
onAfterPageLoad?: (to: string) => Awaitable<void>
/**
* Called after the route changes.
*/
Expand Down
4 changes: 4 additions & 0 deletions docs/zh/reference/runtime-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ interface Router {
* 在页面组件加载前(history 状态更新后)调用。返回 `false` 表示取消导航
*/
onBeforePageLoad?: (to: string) => Awaitable<void | boolean>
/**
* 在页面组件加载后(页面组件实际更新前)调用
*/
onAfterPageLoad?: (to: string) => Awaitable<void>
/**
* 在路由更改后调用
*/
Expand Down
6 changes: 6 additions & 0 deletions src/client/app/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ export interface Router {
* updated). Return `false` to cancel the navigation.
*/
onBeforePageLoad?: (to: string) => Awaitable<void | boolean>
/**
* Called after the page component is loaded (before the page component is updated).
*/
onAfterPageLoad?: (to: string) => Awaitable<void>
/**
* Called after the route changes.
*/
Expand Down Expand Up @@ -94,6 +98,8 @@ export function createRouter(
throw new Error(`Invalid route component: ${comp}`)
}

await router.onAfterPageLoad?.(href)

route.path = inBrowser ? pendingPath : withBase(pendingPath)
route.component = markRaw(comp)
route.data = import.meta.env.PROD
Expand Down

0 comments on commit 315c220

Please sign in to comment.