Skip to content

feat: introduce afterResolve navigation hook #2084

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions docs/en/advanced/navigation-guards.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,19 @@ router.afterEach((to, from) => {
})
```

### Global After Resolve Hooks

> New in 3.1.0

Similarly to `afterEach` hooks, you can also register `afterResolve` hooks that will be triggered after the URL changes.
They don't have a `next` function as they cannot affect the navigation:

```js
router.afterResolve((to, from) => {
// ...
})
```

### Per-Route Guard

You can define `beforeEnter` guards directly on a route's configuration object:
Expand Down Expand Up @@ -151,5 +164,6 @@ beforeRouteLeave (to, from , next) {
8. Call global `beforeResolve` guards (2.5+).
9. Navigation confirmed.
10. Call global `afterEach` hooks.
11. DOM updates triggered.
12. Call callbacks passed to `next` in `beforeRouteEnter` guards with instantiated instances.
11. Call global `afterResolve` hooks after the URL is changed (3.1+).
12. DOM updates triggered.
13. Call callbacks passed to `next` in `beforeRouteEnter` guards with instantiated instances.
12 changes: 12 additions & 0 deletions examples/navigation-guards/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,18 @@ router.beforeEach((to, from, next) => {
}
})

router.afterEach((to, from) => {
console.log(`to: ${to.fullPath}`)
console.log(`from: ${from.fullPath}`)
console.log(`Browser URL: ${window.location.pathname + window.location.hash}`)
})

router.afterResolve((to, from, next) => {
console.log(`to: ${to.fullPath}`)
console.log(`from: ${from.fullPath}`)
console.log(`Browser URL: ${window.location.pathname + window.location.hash}`)
})

new Vue({
router,
template: `
Expand Down
5 changes: 4 additions & 1 deletion src/history/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export class History {
// wait until async components are resolved before
// extracting in-component enter guards
const enterGuards = extractEnterGuards(activated, postEnterCbs, isValid)
const queue = enterGuards.concat(this.router.resolveHooks)
const queue = enterGuards.concat(this.router.beforeResolveHooks)
runQueue(queue, iterator, () => {
if (this.pending !== route) {
return abort()
Expand All @@ -175,6 +175,9 @@ export class History {
onComplete(route)
if (this.router.app) {
this.router.app.$nextTick(() => {
this.router.afterResolveHooks.forEach(hook => {
hook && hook(route, current)
})
postEnterCbs.forEach(cb => { cb() })
})
}
Expand Down
12 changes: 9 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,18 @@ export default class VueRouter {
matcher: Matcher;
fallback: boolean;
beforeHooks: Array<?NavigationGuard>;
resolveHooks: Array<?NavigationGuard>;
beforeResolveHooks: Array<?NavigationGuard>;
afterHooks: Array<?AfterNavigationHook>;
afterResolveHooks: Array<?AfterNavigationHook>;

constructor (options: RouterOptions = {}) {
this.app = null
this.apps = []
this.options = options
this.beforeHooks = []
this.resolveHooks = []
this.beforeResolveHooks = []
this.afterHooks = []
this.afterResolveHooks = []
this.matcher = createMatcher(options.routes || [], this)

let mode = options.mode || 'hash'
Expand Down Expand Up @@ -123,13 +125,17 @@ export default class VueRouter {
}

beforeResolve (fn: Function): Function {
return registerHook(this.resolveHooks, fn)
return registerHook(this.beforeResolveHooks, fn)
}

afterEach (fn: Function): Function {
return registerHook(this.afterHooks, fn)
}

afterResolve (fn: Function): Function {
return registerHook(this.afterResolveHooks, fn)
}

onReady (cb: Function, errorCb?: Function) {
this.history.onReady(cb, errorCb)
}
Expand Down