Skip to content

Commit 82646dc

Browse files
sis0k0hdeshev
authored andcommitted
fix(ns-router-link): add relativeTo navigation extra only if
currentRoute is not empty
1 parent a146e7b commit 82646dc

File tree

2 files changed

+80
-76
lines changed

2 files changed

+80
-76
lines changed

Diff for: nativescript-angular/router/ns-router-link.ts

+79-75
Original file line numberDiff line numberDiff line change
@@ -34,85 +34,89 @@ import { isString } from "tns-core-modules/utils/types";
3434
*/
3535
@Directive({ selector: "[nsRouterLink]" })
3636
export class NSRouterLink implements OnChanges { // tslint:disable-line:directive-class-suffix
37-
private commands: any[] = [];
38-
@Input() target: string;
39-
@Input() queryParams: { [k: string]: any };
40-
@Input() fragment: string;
37+
private commands: any[] = [];
38+
@Input() target: string;
39+
@Input() queryParams: { [k: string]: any };
40+
@Input() fragment: string;
4141

42-
@Input() clearHistory: boolean;
43-
@Input() pageTransition: boolean | string | NavigationTransition = true;
42+
@Input() clearHistory: boolean;
43+
@Input() pageTransition: boolean | string | NavigationTransition = true;
4444

45-
urlTree: UrlTree;
45+
urlTree: UrlTree;
4646

47-
private usePageRoute: boolean;
47+
private usePageRoute: boolean;
4848

49-
private get currentRoute(): ActivatedRoute {
50-
return this.usePageRoute ? this.pageRoute.activatedRoute.getValue() : this.route;
51-
}
52-
53-
constructor(
54-
private router: Router,
55-
private navigator: RouterExtensions,
56-
private route: ActivatedRoute,
57-
@Optional() private pageRoute: PageRoute) {
58-
59-
this.usePageRoute = (this.pageRoute && this.route === this.pageRoute.activatedRoute.getValue());
60-
}
61-
62-
@Input("nsRouterLink")
63-
set params(data: any[] | string) {
64-
if (Array.isArray(data)) {
65-
this.commands = data;
66-
} else {
67-
this.commands = [data];
49+
private get currentRoute(): ActivatedRoute {
50+
return this.usePageRoute ? this.pageRoute.activatedRoute.getValue() : this.route;
6851
}
69-
}
70-
71-
72-
@HostListener("tap")
73-
onTap() {
74-
routerLog("nsRouterLink.tapped: " + this.commands + " usePageRoute: " +
75-
this.usePageRoute + " clearHistory: " + this.clearHistory + " transition: " +
76-
JSON.stringify(this.pageTransition));
77-
78-
const transition = this.getTransition();
79-
80-
let extras: NavigationExtras & NavigationOptions = {
81-
relativeTo: this.currentRoute,
82-
queryParams: this.queryParams,
83-
fragment: this.fragment,
84-
clearHistory: this.clearHistory,
85-
animated: transition.animated,
86-
transition: transition.transition
87-
};
88-
89-
this.navigator.navigate(this.commands, extras);
90-
}
91-
92-
private getTransition(): { animated: boolean, transition?: NavigationTransition } {
93-
if (typeof this.pageTransition === "boolean") {
94-
return { animated: <boolean>this.pageTransition };
95-
} else if (isString(this.pageTransition)) {
96-
if (this.pageTransition === "none" || this.pageTransition === "false") {
97-
return { animated: false };
98-
} else {
99-
return { animated: true, transition: { name: <string>this.pageTransition } };
100-
}
101-
} else {
102-
return {
103-
animated: true,
104-
transition: this.pageTransition
105-
};
106-
}
107-
}
108-
109-
ngOnChanges(_: {}): any {
110-
this.updateUrlTree();
111-
}
11252

113-
private updateUrlTree(): void {
114-
this.urlTree = this.router.createUrlTree(
115-
this.commands,
116-
{ relativeTo: this.currentRoute, queryParams: this.queryParams, fragment: this.fragment });
117-
}
53+
constructor(
54+
private router: Router,
55+
private navigator: RouterExtensions,
56+
private route: ActivatedRoute,
57+
@Optional() private pageRoute: PageRoute) {
58+
59+
this.usePageRoute = (this.pageRoute && this.route === this.pageRoute.activatedRoute.getValue());
60+
}
61+
62+
@Input("nsRouterLink")
63+
set params(data: any[] | string) {
64+
if (Array.isArray(data)) {
65+
this.commands = data;
66+
} else {
67+
this.commands = [data];
68+
}
69+
}
70+
71+
72+
@HostListener("tap")
73+
onTap() {
74+
routerLog("nsRouterLink.tapped: " + this.commands + " usePageRoute: " +
75+
this.usePageRoute + " clearHistory: " + this.clearHistory + " transition: " +
76+
JSON.stringify(this.pageTransition));
77+
78+
const extras = this.getExtras();
79+
this.navigator.navigate(this.commands, extras);
80+
}
81+
82+
private getExtras() {
83+
const transition = this.getTransition();
84+
const extras: NavigationExtras & NavigationOptions = {
85+
queryParams: this.queryParams,
86+
fragment: this.fragment,
87+
clearHistory: this.clearHistory,
88+
animated: transition.animated,
89+
transition: transition.transition,
90+
};
91+
92+
return (<any>Object).assign(extras,
93+
this.currentRoute.toString() !== "Route(url:'', path:'')" && this.currentRoute);
94+
}
95+
96+
private getTransition(): { animated: boolean, transition?: NavigationTransition } {
97+
if (typeof this.pageTransition === "boolean") {
98+
return { animated: <boolean>this.pageTransition };
99+
} else if (isString(this.pageTransition)) {
100+
if (this.pageTransition === "none" || this.pageTransition === "false") {
101+
return { animated: false };
102+
} else {
103+
return { animated: true, transition: { name: <string>this.pageTransition } };
104+
}
105+
} else {
106+
return {
107+
animated: true,
108+
transition: this.pageTransition
109+
};
110+
}
111+
}
112+
113+
ngOnChanges(_: {}): any {
114+
this.updateUrlTree();
115+
}
116+
117+
private updateUrlTree(): void {
118+
this.urlTree = this.router.createUrlTree(
119+
this.commands,
120+
{ relativeTo: this.currentRoute, queryParams: this.queryParams, fragment: this.fragment });
121+
}
118122
}

Diff for: tests/e2e-tests/lazy-load-routing.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,6 @@ describe("lazy load routing", function () {
6161
.should.eventually.exist
6262
.text().should.eventually.equal("Second: lazy-load")
6363
.elementByAccessibilityId("router-location-strategy-states-lazy-load")
64-
.text().should.eventually.equal("/first/lazy-load,/second/lazy-load")
64+
.text().should.eventually.equal("/second/lazy-load")
6565
});
6666
});

0 commit comments

Comments
 (0)