Skip to content

Commit 7433d9d

Browse files
ADjenkovADjenkov
ADjenkov
authored and
ADjenkov
committed
fix(router-extensions): unable to go back with relativeTo param
1 parent 6c91167 commit 7433d9d

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

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

+4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ export class Outlet {
4141
return this.frames.indexOf(frame) > -1;
4242
}
4343

44+
isNSEmptyOutlet(): boolean {
45+
return this.frames.length > 1;
46+
}
47+
4448
peekState(): LocationState {
4549
if (this.states.length > 0) {
4650
return this.states[this.states.length - 1];

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+
let 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)