Skip to content

Commit bb0b48d

Browse files
authored
fix(router-extensions): unable to go back with relativeTo param (#1632)
* fix(router-extensions): unable to go back with relativeTo param * refactor(outlet): introduce isNSEmptyOutlet property
1 parent 5704914 commit bb0b48d

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

Diff for: nativescript-angular/router/ns-location-strategy.ts

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export class Outlet {
2525
path: string;
2626
pathByOutlets: string;
2727
states: Array<LocationState> = [];
28+
isNSEmptyOutlet: boolean;
2829

2930
// Used in reuse-strategy by its children to determine if they should be detached too.
3031
shouldDetach: boolean = true;

Diff for: nativescript-angular/router/page-router-outlet.ts

+1
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ export class PageRouterOutlet implements OnDestroy { // tslint:disable-line:dire
280280
return;
281281
}
282282

283+
this.outlet.isNSEmptyOutlet = this.isEmptyOutlet;
283284
this.locationStrategy.updateOutletFrame(this.outlet, this.frame);
284285

285286
if (this.outlet && this.outlet.isPageNavigationBack) {

Diff for: nativescript-angular/router/router-extensions.ts

+23-11
Original file line numberDiff line numberDiff line change
@@ -93,22 +93,24 @@ export class RouterExtensions {
9393
const outletsToBack: Array<Outlet> = [];
9494
const rootRoute: ActivatedRoute = this.router.routerState.root;
9595
let outlets = options.outlets;
96-
let relativeRoute = options.relativeTo;
96+
let relativeRoute = options.relativeTo || rootRoute;
9797

98-
if (!outlets && relativeRoute) {
99-
outlets = [relativeRoute.outlet];
100-
relativeRoute = relativeRoute.parent || rootRoute;
101-
} else if (!relativeRoute) {
102-
relativeRoute = rootRoute;
98+
const relativeRouteOutlet = this.findOutletByRoute(relativeRoute);
99+
const isNSEmptyOutlet = relativeRouteOutlet && relativeRouteOutlet.isNSEmptyOutlet;
100+
101+
// Lazy named outlet has added 'primary' inner NSEmptyOutlet child.
102+
// Take parent route when `relativeTo` option points to the outer named outlet.
103+
if (isNSEmptyOutlet && relativeRoute.outlet !== "primary") {
104+
relativeRoute = relativeRoute.parent || relativeRoute;
103105
}
104106

105-
for (let index = 0; index < relativeRoute.children.length; index++) {
106-
const currentRoute = relativeRoute.children[index];
107+
const routesToMatch = outlets ? relativeRoute.children : [relativeRoute];
108+
outlets = outlets || [relativeRoute.outlet];
107109

110+
for (let index = 0; index < routesToMatch.length; index++) {
111+
const currentRoute = routesToMatch[index];
108112
if (outlets.some(currentOutlet => currentOutlet === currentRoute.outlet)) {
109-
const currentRouteSnapshop = findTopActivatedRouteNodeForOutlet(currentRoute.snapshot);
110-
const outletKey = this.locationStrategy.getRouteFullPath(currentRouteSnapshop);
111-
let outlet = this.locationStrategy.findOutletByKey(outletKey);
113+
const outlet = this.findOutletByRoute(currentRoute);
112114

113115
if (outlet) {
114116
outletsToBack.push(outlet);
@@ -118,4 +120,14 @@ export class RouterExtensions {
118120

119121
return { outletsToBack: outletsToBack, outlets: outlets };
120122
}
123+
124+
private findOutletByRoute(currentRoute: ActivatedRoute): Outlet {
125+
let outlet;
126+
127+
const currentRouteSnapshop = findTopActivatedRouteNodeForOutlet(currentRoute.snapshot);
128+
const outletKey = this.locationStrategy.getRouteFullPath(currentRouteSnapshop);
129+
outlet = this.locationStrategy.findOutletByKey(outletKey);
130+
131+
return outlet;
132+
}
121133
}

0 commit comments

Comments
 (0)