fix(router): CanDeactivate receives a wrong component

Closes #12592
This commit is contained in:
vsavkin 2016-11-01 12:11:42 -07:00
parent 6fda97287e
commit 830a780cb3
2 changed files with 27 additions and 9 deletions

View File

@ -867,14 +867,25 @@ export class PreActivation {
private deactiveRouteAndItsChildren(
route: TreeNode<ActivatedRouteSnapshot>, outlet: RouterOutlet): void {
const prevChildren: {[key: string]: any} = nodeChildrenAsMap(route);
const r = route.value;
forEach(prevChildren, (v: any, k: string) => {
const childOutlet = outlet ? outlet.outletMap._outlets[k] : null;
this.deactiveRouteAndItsChildren(v, childOutlet);
if (!r.component) {
this.deactiveRouteAndItsChildren(v, outlet);
} else if (!!outlet) {
this.deactiveRouteAndItsChildren(v, outlet.outletMap._outlets[k]);
} else {
this.deactiveRouteAndItsChildren(v, null);
}
});
const component = outlet && outlet.isActivated ? outlet.component : null;
this.checks.push(new CanDeactivate(component, route.value));
if (!r.component) {
this.checks.push(new CanDeactivate(null, r));
} else if (outlet && outlet.isActivated) {
this.checks.push(new CanDeactivate(outlet.component, r));
} else {
this.checks.push(new CanDeactivate(null, r));
}
}
private runCanActivate(future: ActivatedRouteSnapshot): Observable<boolean> {

View File

@ -1245,7 +1245,7 @@ describe('Integration', () => {
{
provide: 'RecordingDeactivate',
useValue: (c: any, a: ActivatedRouteSnapshot, b: RouterStateSnapshot) => {
log.push(['Deactivate', a.routeConfig.path]);
log.push({path: a.routeConfig.path, component: c});
return true;
}
},
@ -1290,7 +1290,11 @@ describe('Integration', () => {
children: [{
path: 'child',
canDeactivate: ['RecordingDeactivate'],
children: [{path: 'simple', component: SimpleCmp}]
children: [{
path: 'simple',
component: SimpleCmp,
canDeactivate: ['RecordingDeactivate']
}]
}]
}]
},
@ -1304,9 +1308,12 @@ describe('Integration', () => {
router.navigateByUrl('/simple');
advance(fixture);
expect(log).toEqual([
['Deactivate', 'child'], ['Deactivate', 'parent'], ['Deactivate', 'grandparent']
const child = fixture.debugElement.children[1].componentInstance;
expect(log.map((a: any) => a.path)).toEqual([
'simple', 'child', 'parent', 'grandparent'
]);
expect(log.map((a: any) => a.component)).toEqual([child, null, null, null]);
})));
it('works with aux routes',
@ -1333,7 +1340,7 @@ describe('Integration', () => {
router.navigate(['two-outlets', {outlets: {aux: null}}]);
advance(fixture);
expect(log).toEqual([['Deactivate', 'b']]);
expect(log.map((a: any) => a.path)).toEqual(['b']);
expect(location.path()).toEqual('/two-outlets/(a)');
})));