Skip to content

Commit 007337a

Browse files
committed
feat: Add beforeEnter support in routes list acces
1 parent f013415 commit 007337a

File tree

4 files changed

+34
-14
lines changed

4 files changed

+34
-14
lines changed

src/router-guard-service.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import {
22
GuardReturnContext,
3-
NavigationGuardCallback,
43
NextContext,
54
} from "./typings/router-guards-service";
65
import { Route } from "./typings/router-service";
7-
import { NavigationHookAfter, RouteLocationNormalized } from "vue-router";
6+
import { NavigationGuard, NavigationHookAfter, RouteLocationNormalized } from "vue-router";
87

98
/**
109
* Single Guard Service
@@ -16,10 +15,10 @@ export class RouterGuardService {
1615
* List of Guard Callbacks
1716
*
1817
* @private
19-
* @type {NavigationGuardCallback[]}
18+
* @type {NavigationGuard[]}
2019
* @memberof RouterGuardService
2120
*/
22-
private guardCallbacks: NavigationGuardCallback[] = [];
21+
private guardCallbacks: NavigationGuard[] = [];
2322

2423
/**
2524
* Whether current navigation is cancelled from inside of guard
@@ -77,10 +76,10 @@ export class RouterGuardService {
7776
/**
7877
* Add a guard
7978
*
80-
* @param {NavigationGuardCallback} callback The callback to be added to the guard
79+
* @param {NavigationGuard} callback The callback to be added to the guard
8180
* @returns {void}
8281
*/
83-
public add(callback: NavigationGuardCallback): void {
82+
public add(callback: NavigationGuard): void {
8483
this.guardCallbacks.push(callback);
8584
}
8685

src/router-mixin.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Vue from "nativescript-vue";
22
import { Page } from "@nativescript/core/ui/page";
3+
import { RouterService } from "./router-service";
34

45
export default {
56
nextCallbacks: [],
@@ -62,8 +63,8 @@ export default {
6263
return;
6364
}
6465

65-
const to = this.$router.getNewRoute();
66-
const from = this.$router.getCurrentRoute();
66+
const to = (this.$router as RouterService).getNewRoute();
67+
const from = (this.$router as RouterService).getCurrentRoute();
6768

6869
if (this.$options.beforeRouteLeave) {
6970
this.$options.beforeRouteLeave.call(
@@ -90,8 +91,8 @@ export default {
9091
},
9192

9293
onNavigatingTo(data) {
93-
const to = this.$router.getNewRoute();
94-
const from = this.$router.getCurrentRoute();
94+
const to = (this.$router as RouterService).getNewRoute();
95+
const from = (this.$router as RouterService).getCurrentRoute();
9596

9697
if (this.$options.beforeRouteUpdate && to && from && to.path === from.path) {
9798
this.$options.beforeRouteUpdate.call(
@@ -102,6 +103,28 @@ export default {
102103
);
103104
}
104105

106+
if (to.beforeEnter && typeof to.beforeEnter === "function") {
107+
const next = (vmContext) => {
108+
if (typeof vmContext === "undefined") {
109+
return;
110+
}
111+
112+
// Do not invoke callback immediately even though instance of new component is provided
113+
// This is to keep cb invocation order in sync with Vue-Router
114+
if (typeof vmContext === "function") {
115+
this.$options.nextCallbacks.push(vmContext);
116+
}
117+
};
118+
119+
this.$options.beforeEnter.call(
120+
this,
121+
to,
122+
from,
123+
next,
124+
data.object.navigationContext
125+
);
126+
}
127+
105128
if (this.$options.beforeRouteEnter) {
106129
const next = (vmContext) => {
107130
if (typeof vmContext === "undefined") {

src/typings/router-guards-service.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ export type BeforeRouteEnter = (to: Route, from?: Route, next?: NextContext) =>
2020

2121
export type BeforeRouteLeave = (to: Route, from?: Route, next?: NextContext) => void;
2222

23-
export type NavigationGuardCallback = NavigationGuard;
24-
2523
export interface GuardsInitArgs {
2624
to: Route;
2725
from?: Route;

src/typings/router-service.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
RouterOptions,
3+
NavigationGuardNext,
34
} from 'vue-router';
45
import {
56
NavigationEntryVue,
@@ -36,8 +37,7 @@ export interface Route extends RouteOptions {
3637
component?: Component;
3738
children?: RouteChildren;
3839

39-
beforeRouteEnter?: (to: Route, from: Route, next?: (vm: any) => void) => boolean;
40-
beforeRouteLeave?: (to: Route, from: Route, next?: (vm: any) => void) => boolean;
40+
beforeEnter?: (to: Route, from: Route, next?: NavigationGuardNext) => void;
4141
}
4242

4343
export interface RouterServiceOptions {

0 commit comments

Comments
 (0)