Skip to content

Commit 5ea15b9

Browse files
author
Ciro Nunes
committed
feat: introduce afterResolve navigation hook
1 parent 768f6b7 commit 5ea15b9

File tree

4 files changed

+42
-6
lines changed

4 files changed

+42
-6
lines changed

Diff for: docs/en/advanced/navigation-guards.md

+16-2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,19 @@ router.afterEach((to, from) => {
5252
})
5353
```
5454

55+
### Global After Resolve Hooks
56+
57+
> New in 3.1.0
58+
59+
Similarly to `afterEach` hooks, you can also register `afterResolve` hooks that will be triggered after the URL changes.
60+
They don't have a `next` function as they cannot affect the navigation:
61+
62+
```js
63+
router.afterResolve((to, from) => {
64+
// ...
65+
})
66+
```
67+
5568
### Per-Route Guard
5669

5770
You can define `beforeEnter` guards directly on a route's configuration object:
@@ -151,5 +164,6 @@ beforeRouteLeave (to, from , next) {
151164
8. Call global `beforeResolve` guards (2.5+).
152165
9. Navigation confirmed.
153166
10. Call global `afterEach` hooks.
154-
11. DOM updates triggered.
155-
12. Call callbacks passed to `next` in `beforeRouteEnter` guards with instantiated instances.
167+
11. Call global `afterResolve` hooks after the URL is changed (3.1+).
168+
12. DOM updates triggered.
169+
13. Call callbacks passed to `next` in `beforeRouteEnter` guards with instantiated instances.

Diff for: examples/navigation-guards/app.js

+12
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,18 @@ router.beforeEach((to, from, next) => {
126126
}
127127
})
128128

129+
router.afterEach((to, from) => {
130+
console.log(`to: ${to.fullPath}`)
131+
console.log(`from: ${from.fullPath}`)
132+
console.log(`Browser URL: ${window.location.pathname + window.location.hash}`)
133+
})
134+
135+
router.afterResolve((to, from, next) => {
136+
console.log(`to: ${to.fullPath}`)
137+
console.log(`from: ${from.fullPath}`)
138+
console.log(`Browser URL: ${window.location.pathname + window.location.hash}`)
139+
})
140+
129141
new Vue({
130142
router,
131143
template: `

Diff for: src/history/base.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ export class History {
166166
// wait until async components are resolved before
167167
// extracting in-component enter guards
168168
const enterGuards = extractEnterGuards(activated, postEnterCbs, isValid)
169-
const queue = enterGuards.concat(this.router.resolveHooks)
169+
const queue = enterGuards.concat(this.router.beforeResolveHooks)
170170
runQueue(queue, iterator, () => {
171171
if (this.pending !== route) {
172172
return abort()
@@ -175,6 +175,10 @@ export class History {
175175
onComplete(route)
176176
if (this.router.app) {
177177
this.router.app.$nextTick(() => {
178+
// runQueue(this.router.afterResolveHooks, iterator, () => {})
179+
this.router.afterResolveHooks.forEach(hook => {
180+
hook && hook(route, current)
181+
})
178182
postEnterCbs.forEach(cb => { cb() })
179183
})
180184
}

Diff for: src/index.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,18 @@ export default class VueRouter {
2929
matcher: Matcher;
3030
fallback: boolean;
3131
beforeHooks: Array<?NavigationGuard>;
32-
resolveHooks: Array<?NavigationGuard>;
32+
beforeResolveHooks: Array<?NavigationGuard>;
3333
afterHooks: Array<?AfterNavigationHook>;
34+
afterResolveHooks: Array<?AfterNavigationHook>;
3435

3536
constructor (options: RouterOptions = {}) {
3637
this.app = null
3738
this.apps = []
3839
this.options = options
3940
this.beforeHooks = []
40-
this.resolveHooks = []
41+
this.beforeResolveHooks = []
4142
this.afterHooks = []
43+
this.afterResolveHooks = []
4244
this.matcher = createMatcher(options.routes || [], this)
4345

4446
let mode = options.mode || 'hash'
@@ -123,13 +125,17 @@ export default class VueRouter {
123125
}
124126

125127
beforeResolve (fn: Function): Function {
126-
return registerHook(this.resolveHooks, fn)
128+
return registerHook(this.beforeResolveHooks, fn)
127129
}
128130

129131
afterEach (fn: Function): Function {
130132
return registerHook(this.afterHooks, fn)
131133
}
132134

135+
afterResolve (fn: Function): Function {
136+
return registerHook(this.afterResolveHooks, fn)
137+
}
138+
133139
onReady (cb: Function, errorCb?: Function) {
134140
this.history.onReady(cb, errorCb)
135141
}

0 commit comments

Comments
 (0)