Skip to content
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

[3.x] Implement onAbort especially for legacy 3.4.2 (3.x branch) without changing logic on 4.x branches #3288

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/api/README.md
Expand Up @@ -438,6 +438,18 @@ Register a callback which will be called when an error is caught during a route

- An error occurred when trying to resolve an async component that is required to render a route.

### router.onAbort

Signature:

```js
router.onAbort(callback)
```

Register a callback which will be called when an a route navigation return false (`next(false)`).

Note: this is only supported in 3.4.2+ and < 4.0 versions.

## The Route Object

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.
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/advanced/navigation-guards.md
Expand Up @@ -30,7 +30,7 @@ Every guard function receives three arguments:

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

- **`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.
- **`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).

- **`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)

Expand Down
36 changes: 27 additions & 9 deletions src/history/base.js
Expand Up @@ -31,6 +31,7 @@ export class History {
readyCbs: Array<Function>
readyErrorCbs: Array<Function>
errorCbs: Array<Function>
abortCbs: Array<Function>
listeners: Array<Function>
cleanupListeners: Function

Expand All @@ -56,6 +57,7 @@ export class History {
this.readyCbs = []
this.readyErrorCbs = []
this.errorCbs = []
this.abortCbs = []
this.listeners = []
}

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

onAbort (abortCb: Function) {
this.abortCbs.push(abortCb)
}

transitionTo (
location: RawLocation,
onComplete?: Function,
Expand Down Expand Up @@ -141,15 +147,27 @@ export class History {
// changed after adding errors with
// https://github.com/vuejs/vue-router/pull/3047 before that change,
// redirect and aborted navigation would produce an err == null
if (!isNavigationFailure(err) && isError(err)) {
if (this.errorCbs.length) {
this.errorCbs.forEach(cb => {
cb(err)
})
} else {
warn(false, 'uncaught error during route navigation:')
console.error(err)
}
if (!isNavigationFailure(err)) {
if(isError(err)) {
if (this.errorCbs.length) {
this.errorCbs.forEach(cb => {
cb(err)
})
} else {
warn(false, 'uncaught error during route navigation:')
console.error(err)
}
}
else if(err === false) {
if (this.abortCbs.length) {
this.abortCbs.forEach(cb => {
cb(err)
})
} else {
warn(false, 'uncaught error during route navigation:')
console.error(err)
}
}
}
onAbort && onAbort(err)
}
Expand Down
4 changes: 4 additions & 0 deletions src/index.js
Expand Up @@ -167,6 +167,10 @@ export default class VueRouter {
this.history.onError(errorCb)
}

onAbort (abortCb: Function) {
this.history.onAbort(abortCb)
}

push (location: RawLocation, onComplete?: Function, onAbort?: Function) {
// $flow-disable-line
if (!onComplete && !onAbort && typeof Promise !== 'undefined') {
Expand Down
1 change: 1 addition & 0 deletions types/router.d.ts
Expand Up @@ -46,6 +46,7 @@ export declare class VueRouter {
getMatchedComponents(to?: RawLocation | Route): Component[]
onReady(cb: Function, errorCb?: ErrorHandler): void
onError(cb: ErrorHandler): void
onAbort(cb: ErrorHandler): void
addRoutes(routes: RouteConfig[]): void
resolve(
to: RawLocation,
Expand Down