Skip to content

Commit 76d6f5f

Browse files
committed
fix(router): canDeactivate should not change the url when returns false
Closes angular#8360
1 parent 0f1b370 commit 76d6f5f

2 files changed

Lines changed: 12 additions & 7 deletions

File tree

modules/angular2/src/alt_router/router.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,12 @@ export class Router {
5959
.then(currTree => {
6060
return new _LoadSegments(currTree, this._prevTree)
6161
.load(this._routerOutletMap, this._rootComponent)
62-
.then(_ => {
63-
this._prevTree = currTree;
64-
this._location.go(this._urlSerializer.serialize(this._urlTree));
65-
this._changes.emit(null);
62+
.then(updated => {
63+
if (updated) {
64+
this._prevTree = currTree;
65+
this._location.go(this._urlSerializer.serialize(this._urlTree));
66+
this._changes.emit(null);
67+
}
6668
});
6769
});
6870
}
@@ -90,7 +92,7 @@ class _LoadSegments {
9092

9193
constructor(private currTree: Tree<RouteSegment>, private prevTree: Tree<RouteSegment>) {}
9294

93-
load(parentOutletMap: RouterOutletMap, rootComponent: Object): Promise<void> {
95+
load(parentOutletMap: RouterOutletMap, rootComponent: Object): Promise<boolean> {
9496
let prevRoot = isPresent(this.prevTree) ? rootNode(this.prevTree) : null;
9597
let currRoot = rootNode(this.currTree);
9698

@@ -100,6 +102,7 @@ class _LoadSegments {
100102
if (res) {
101103
this.loadChildSegments(currRoot, prevRoot, parentOutletMap, [rootComponent]);
102104
}
105+
return res;
103106
});
104107
}
105108

@@ -189,7 +192,7 @@ class _LoadSegments {
189192
}
190193

191194
private unloadOutlet(outlet: RouterOutlet, components: Object[]): void {
192-
if (outlet.isLoaded) {
195+
if (isPresent(outlet) && outlet.isLoaded) {
193196
StringMapWrapper.forEach(outlet.outletMap._outlets,
194197
(v, k) => this.unloadOutlet(v, components));
195198
if (this.performMutation) {

modules/angular2/test/alt_router/integration_spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ export function main() {
127127
})));
128128

129129
it('should not unload the route if can deactivate returns false',
130-
fakeAsync(inject([Router, TestComponentBuilder], (router, tcb) => {
130+
fakeAsync(inject([Router, TestComponentBuilder, Location], (router, tcb, location) => {
131131
let fixture = tcb.createFakeAsync(RootCmp);
132132

133133
router.navigateByUrl('/team/22/cannotDeactivate');
@@ -138,6 +138,8 @@ export function main() {
138138

139139
expect(fixture.debugElement.nativeElement)
140140
.toHaveText('team 22 { cannotDeactivate, aux: }');
141+
142+
expect(location.path()).toEqual('/team/22/cannotDeactivate');
141143
})));
142144

143145
if (DOM.supportsDOMEvents()) {

0 commit comments

Comments
 (0)