Skip to content

Commit 7a2b354

Browse files
author
shel
committed
Implement onAbort especially for legacy 3.4.2 (3.x branch)
1 parent 5c4221a commit 7a2b354

File tree

5 files changed

+45
-10
lines changed

5 files changed

+45
-10
lines changed

docs/api/README.md

+12
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,18 @@ Register a callback which will be called when an error is caught during a route
438438
439439
- An error occurred when trying to resolve an async component that is required to render a route.
440440
441+
### router.onAbort
442+
443+
Signature:
444+
445+
```js
446+
router.onAbort(callback)
447+
```
448+
449+
Register a callback which will be called when an a route navigation return false (`next(false)`).
450+
451+
Note: this is only supported in 3.4.2+ and < 4.0 versions.
452+
441453
## The Route Object
442454
443455
A **route object** represents the state of the current active route. It contains parsed information of the current URL and the **route records** matched by the URL.

docs/guide/advanced/navigation-guards.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Every guard function receives three arguments:
3030

3131
- **`next()`**: move on to the next hook in the pipeline. If no hooks are left, the navigation is **confirmed**.
3232

33-
- **`next(false)`**: abort the current navigation. If the browser URL was changed (either manually by the user or via back button), it will be reset to that of the `from` route.
33+
- **`next(false)`**: abort the current navigation. If the browser URL was changed (either manually by the user or via back button), it will be reset to that of the `from` route. (3.4.2+ and < 4.0) will be passed to callbacks registered via [`router.onAbort()`](../../api/#router-onabort).
3434

3535
- **`next('/')` or `next({ path: '/' })`**: redirect to a different location. The current navigation will be aborted and a new one will be started. You can pass any location object to `next`, which allows you to specify options like `replace: true`, `name: 'home'` and any option used in [`router-link`'s `to` prop](../../api/#to) or [`router.push`](../../api/#router-push)
3636

src/history/base.js

+27-9
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export class History {
3131
readyCbs: Array<Function>
3232
readyErrorCbs: Array<Function>
3333
errorCbs: Array<Function>
34+
abortCbs: Array<Function>
3435
listeners: Array<Function>
3536
cleanupListeners: Function
3637

@@ -56,6 +57,7 @@ export class History {
5657
this.readyCbs = []
5758
this.readyErrorCbs = []
5859
this.errorCbs = []
60+
this.abortCbs = []
5961
this.listeners = []
6062
}
6163

@@ -78,6 +80,10 @@ export class History {
7880
this.errorCbs.push(errorCb)
7981
}
8082

83+
onAbort (abortCb: Function) {
84+
this.abortCbs.push(abortCb)
85+
}
86+
8187
transitionTo (
8288
location: RawLocation,
8389
onComplete?: Function,
@@ -141,15 +147,27 @@ export class History {
141147
// changed after adding errors with
142148
// https://github.com/vuejs/vue-router/pull/3047 before that change,
143149
// redirect and aborted navigation would produce an err == null
144-
if (!isNavigationFailure(err) && isError(err)) {
145-
if (this.errorCbs.length) {
146-
this.errorCbs.forEach(cb => {
147-
cb(err)
148-
})
149-
} else {
150-
warn(false, 'uncaught error during route navigation:')
151-
console.error(err)
152-
}
150+
if (!isNavigationFailure(err)) {
151+
if(isError(err)) {
152+
if (this.errorCbs.length) {
153+
this.errorCbs.forEach(cb => {
154+
cb(err)
155+
})
156+
} else {
157+
warn(false, 'uncaught error during route navigation:')
158+
console.error(err)
159+
}
160+
}
161+
else if(err === false) {
162+
if (this.abortCbs.length) {
163+
this.abortCbs.forEach(cb => {
164+
cb(err)
165+
})
166+
} else {
167+
warn(false, 'uncaught error during route navigation:')
168+
console.error(err)
169+
}
170+
}
153171
}
154172
onAbort && onAbort(err)
155173
}

src/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ export default class VueRouter {
167167
this.history.onError(errorCb)
168168
}
169169

170+
onAbort (abortCb: Function) {
171+
this.history.onAbort(abortCb)
172+
}
173+
170174
push (location: RawLocation, onComplete?: Function, onAbort?: Function) {
171175
// $flow-disable-line
172176
if (!onComplete && !onAbort && typeof Promise !== 'undefined') {

types/router.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export declare class VueRouter {
4646
getMatchedComponents(to?: RawLocation | Route): Component[]
4747
onReady(cb: Function, errorCb?: ErrorHandler): void
4848
onError(cb: ErrorHandler): void
49+
onAbort(cb: ErrorHandler): void
4950
addRoutes(routes: RouteConfig[]): void
5051
resolve(
5152
to: RawLocation,

0 commit comments

Comments
 (0)