Skip to content

Commit 2180572

Browse files
committed
Fix the navigation using extras like clearHistory to a lazy loaded module.
1 parent b6edf4e commit 2180572

File tree

4 files changed

+62
-13
lines changed

4 files changed

+62
-13
lines changed

nativescript-angular/router.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { NgModule, ModuleWithProviders, NO_ERRORS_SCHEMA } from "@angular/core";
1+
import { NgModule, ModuleWithProviders, NO_ERRORS_SCHEMA, Optional, SkipSelf } from "@angular/core";
22
import { RouterModule, Routes, ExtraOptions } from "@angular/router";
33
import { LocationStrategy, PlatformLocation } from "@angular/common";
4+
import { Frame } from "ui/frame";
45
import { NSRouterLink } from "./router/ns-router-link";
56
import { NSRouterLinkActive } from "./router/ns-router-link-active";
67
import { PageRouterOutlet } from "./router/page-router-outlet";
@@ -21,7 +22,11 @@ export type LocationState = LocationState;
2122
PageRouterOutlet
2223
],
2324
providers: [
24-
NSLocationStrategy,
25+
{
26+
provide: NSLocationStrategy,
27+
useFactory: provideLocationStrategy,
28+
deps: [[NSLocationStrategy, new Optional(), new SkipSelf()], Frame]
29+
},
2530
{ provide: LocationStrategy, useExisting: NSLocationStrategy },
2631
NativescriptPlatformLocation,
2732
{ provide: PlatformLocation, useClass: NativescriptPlatformLocation },
@@ -48,3 +53,7 @@ export class NativeScriptRouterModule {
4853
return RouterModule.forChild(routes);
4954
}
5055
}
56+
57+
export function provideLocationStrategy(locationStrategy: NSLocationStrategy, frame: Frame): NSLocationStrategy {
58+
return locationStrategy ? locationStrategy : new NSLocationStrategy(frame);
59+
}

tests/app/first.component.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Router, ActivatedRoute } from '@angular/router';
22
import { Component, Inject } from "@angular/core";
3+
import { RouterExtensions } from "nativescript-angular/router";
34
import { HOOKS_LOG, BaseComponent } from "./base.component";
45
import { BehaviorSubject } from "rxjs/BehaviorSubject";
56

@@ -8,22 +9,34 @@ import { BehaviorSubject } from "rxjs/BehaviorSubject";
89
template: `
910
<StackLayout>
1011
<Label [automationText]="'first-' + id" [text]="'First: ' + id"></Label>
11-
<Button [automationText]="'first-navigate-' + id" text="Go to second" (tap)="gotoSecond()"></Button>
12-
<TextView [automationText]="'hooks-log-' + id" [text]="hooksLog | async"></TextView>
13-
<TextView [text]="'hooks-log-' + id"></TextView>
12+
<StackLayout orientation="horizontal">
13+
<Button [automationText]="'first-navigate-' + id" text="Go to second" (tap)="gotoSecond()"></Button>
14+
<Button [automationText]="'first-navigate-clear-history-' + id" text="With clear history"
15+
(tap)="gotoSecondAndClearHistory()">
16+
</Button>
17+
</StackLayout>
18+
<Label [automationText]="'hooks-log-' + id" [text]="hooksLog | async"></Label>
19+
<Label [text]="'hooks-log-' + id"></Label>
1420
</StackLayout>
1521
`
1622
})
1723
export class FirstComponent extends BaseComponent {
1824
protected name = "first";
1925
public id: string = "";
2026

21-
constructor(private router: Router, private routeData: ActivatedRoute, @Inject(HOOKS_LOG) hooksLog: BehaviorSubject<Array<string>>) {
27+
constructor(private routerExtensions: RouterExtensions,
28+
private routeData: ActivatedRoute,
29+
@Inject(HOOKS_LOG) hooksLog: BehaviorSubject<Array<string>>
30+
) {
2231
super(hooksLog);
2332
this.id = routeData.snapshot.params["id"];
2433
}
2534

2635
gotoSecond() {
27-
this.router.navigateByUrl("/second/" + this.id);
36+
this.routerExtensions.navigateByUrl("/second/" + this.id);
37+
}
38+
39+
gotoSecondAndClearHistory() {
40+
this.routerExtensions.navigateByUrl("/second/" + this.id, { clearHistory: true })
2841
}
2942
}

tests/app/second.component.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Router, ActivatedRoute } from "@angular/router";
2-
import { Component, Inject } from "@angular/core";
2+
import { Component, Inject, OnInit } from "@angular/core";
33
import { Location } from "@angular/common";
4+
import { RouterExtensions } from "nativescript-angular/router";
45
import { HOOKS_LOG, BaseComponent } from "./base.component";
56
import { BehaviorSubject } from "rxjs/BehaviorSubject";
67

@@ -10,20 +11,31 @@ import { BehaviorSubject } from "rxjs/BehaviorSubject";
1011
<StackLayout>
1112
<Label [automationText]="'second-' + id" [text]="'Second: ' + id"></Label>
1213
<Button [automationText]="'second-navigate-back-' + id" text="Go to first" (tap)="goBack()"></Button>
13-
<TextView [automationText]="'hooks-log-' + id" [text]="hooksLog | async"></TextView>
14-
<TextView [text]="'hooks-log-' + id"></TextView>
14+
<Label [automationText]="'hooks-log-' + id" [text]="hooksLog | async"></Label>
15+
<Label [text]="'hooks-log-' + id"></Label>
16+
<Label [automationText]="'router-location-strategy-states-' + id" [text]="routerLocationStrategystates"></Label>
1517
</StackLayout>
1618
`
1719
})
18-
export class SecondComponent extends BaseComponent {
20+
export class SecondComponent extends BaseComponent implements OnInit {
1921
protected name = "second";
2022
public id: string = "";
23+
protected routerLocationStrategystates = "";
2124

22-
constructor(private router: Router, private location: Location, private routeData: ActivatedRoute, @Inject(HOOKS_LOG) hooksLog: BehaviorSubject<Array<string>>) {
25+
constructor(private routerExtensions: RouterExtensions,
26+
private location: Location,
27+
private routeData: ActivatedRoute,
28+
@Inject(HOOKS_LOG) hooksLog: BehaviorSubject<Array<string>>
29+
) {
2330
super(hooksLog);
2431
this.id = routeData.snapshot.params["id"];
2532
}
2633

34+
ngOnInit() {
35+
super.ngOnInit();
36+
this.routerLocationStrategystates = this.routerExtensions.locationStrategy._getStates().map(state => state.url).join(",");
37+
}
38+
2739
goBack() {
2840
this.location.back();
2941
}

tests/e2e-tests/lazy-load-routing.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ describe("lazy load routing", function () {
3838
.tap()
3939
.elementByAccessibilityId("second-lazy-load")
4040
.should.eventually.exist
41-
.text().should.eventually.equal("Second: lazy-load")
41+
.text().should.eventually.equal("Second: lazy-load")
42+
.elementByAccessibilityId("router-location-strategy-states-lazy-load")
43+
.text().should.eventually.equal("/first/lazy-load,/second/lazy-load")
4244
.elementByAccessibilityId("second-navigate-back-lazy-load")
4345
.should.eventually.exist
4446
.tap()
@@ -48,4 +50,17 @@ describe("lazy load routing", function () {
4850
.elementByAccessibilityId("hooks-log-lazy-load")
4951
.text().should.eventually.equal(expectedHookLog)
5052
});
53+
54+
it("navigates and clear history", function() {
55+
return driver
56+
.waitForElementByAccessibilityId("first-navigate-lazy-load", 300000)
57+
.elementByAccessibilityId("first-navigate-clear-history-lazy-load")
58+
.should.eventually.exist
59+
.tap()
60+
.elementByAccessibilityId("second-lazy-load")
61+
.should.eventually.exist
62+
.text().should.eventually.equal("Second: lazy-load")
63+
.elementByAccessibilityId("router-location-strategy-states-lazy-load")
64+
.text().should.eventually.equal("/second/lazy-load")
65+
});
5166
});

0 commit comments

Comments
 (0)