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( private deactiveRouteAndItsChildren(
route: TreeNode<ActivatedRouteSnapshot>, outlet: RouterOutlet): void { route: TreeNode<ActivatedRouteSnapshot>, outlet: RouterOutlet): void {
const prevChildren: {[key: string]: any} = nodeChildrenAsMap(route); const prevChildren: {[key: string]: any} = nodeChildrenAsMap(route);
const r = route.value;
forEach(prevChildren, (v: any, k: string) => { forEach(prevChildren, (v: any, k: string) => {
const childOutlet = outlet ? outlet.outletMap._outlets[k] : null; if (!r.component) {
this.deactiveRouteAndItsChildren(v, childOutlet); 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; if (!r.component) {
this.checks.push(new CanDeactivate(component, route.value)); 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> { private runCanActivate(future: ActivatedRouteSnapshot): Observable<boolean> {

View File

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