Skip to content

fix(router-extensions): unable to go back with relativeTo param #1632

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

Merged
merged 4 commits into from
Nov 26, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions nativescript-angular/router/ns-location-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ export class Outlet {
return this.frames.indexOf(frame) > -1;
}

isNSEmptyOutlet(): boolean {
return this.frames.length > 1;
}

peekState(): LocationState {
if (this.states.length > 0) {
return this.states[this.states.length - 1];
Expand Down
34 changes: 23 additions & 11 deletions nativescript-angular/router/router-extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,22 +93,24 @@ export class RouterExtensions {
const outletsToBack: Array<Outlet> = [];
const rootRoute: ActivatedRoute = this.router.routerState.root;
let outlets = options.outlets;
let relativeRoute = options.relativeTo;
let relativeRoute = options.relativeTo || rootRoute;

if (!outlets && relativeRoute) {
outlets = [relativeRoute.outlet];
relativeRoute = relativeRoute.parent || rootRoute;
} else if (!relativeRoute) {
relativeRoute = rootRoute;
const relativeRouteOutlet = this.findOutletByRoute(relativeRoute);
const isNSEmptyOutlet = relativeRouteOutlet && relativeRouteOutlet.isNSEmptyOutlet();

// Lazy named outlet has added 'primary' inner NSEmptyOutlet child.
// Take parent route when `relativeTo` option points to the outer named outlet.
if (isNSEmptyOutlet && relativeRoute.outlet !== "primary") {
relativeRoute = relativeRoute.parent || relativeRoute;
}

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

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

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

return { outletsToBack: outletsToBack, outlets: outlets };
}

private findOutletByRoute(currentRoute: ActivatedRoute): Outlet {
let outlet;

const currentRouteSnapshop = findTopActivatedRouteNodeForOutlet(currentRoute.snapshot);
const outletKey = this.locationStrategy.getRouteFullPath(currentRouteSnapshop);
outlet = this.locationStrategy.findOutletByKey(outletKey);

return outlet;
}
}