fix(router): fix nested deactivation

This commit is contained in:
vsavkin 2016-06-09 11:02:57 -07:00
parent 820eeb49d1
commit 1f3f8ef6c8
2 changed files with 12 additions and 3 deletions

View File

@ -1,10 +1,12 @@
import {Attribute, ComponentFactory, ComponentRef, Directive, ReflectiveInjector, ResolvedReflectiveProvider, ViewContainerRef} from '@angular/core'; import {Attribute, ComponentFactory, ComponentRef, Directive, ReflectiveInjector, ResolvedReflectiveProvider, ViewContainerRef} from '@angular/core';
import {RouterOutletMap} from '../router_outlet_map'; import {RouterOutletMap} from '../router_outlet_map';
import {ActivatedRoute} from '../router_state';
import {PRIMARY_OUTLET} from '../shared'; import {PRIMARY_OUTLET} from '../shared';
@Directive({selector: 'router-outlet'}) @Directive({selector: 'router-outlet'})
export class RouterOutlet { export class RouterOutlet {
private activated: ComponentRef<any>|null; private activated: ComponentRef<any>|null;
private _activatedRoute: ActivatedRoute|null;
public outletMap: RouterOutletMap; public outletMap: RouterOutletMap;
/** /**
@ -21,6 +23,10 @@ export class RouterOutlet {
if (!this.activated) throw new Error('Outlet is not activated'); if (!this.activated) throw new Error('Outlet is not activated');
return this.activated.instance; return this.activated.instance;
} }
get activatedRoute(): ActivatedRoute {
if (!this.activated) throw new Error('Outlet is not activated');
return this._activatedRoute;
}
deactivate(): void { deactivate(): void {
if (this.activated) { if (this.activated) {
@ -30,9 +36,10 @@ export class RouterOutlet {
} }
activate( activate(
factory: ComponentFactory<any>, providers: ResolvedReflectiveProvider[], factory: ComponentFactory<any>, activatedRoute: ActivatedRoute, providers: ResolvedReflectiveProvider[],
outletMap: RouterOutletMap): void { outletMap: RouterOutletMap): void {
this.outletMap = outletMap; this.outletMap = outletMap;
this._activatedRoute = activatedRoute;
const inj = ReflectiveInjector.fromResolvedProviders(providers, this.location.parentInjector); const inj = ReflectiveInjector.fromResolvedProviders(providers, this.location.parentInjector);
this.activated = this.location.createComponent(factory, this.location.length, inj, []); this.activated = this.location.createComponent(factory, this.location.length, inj, []);
} }

View File

@ -231,6 +231,7 @@ export class Router {
private runNavigate(url: UrlTree, pop: boolean, id: number): Promise<boolean> { private runNavigate(url: UrlTree, pop: boolean, id: number): Promise<boolean> {
if (id !== this.navigationId) { if (id !== this.navigationId) {
this.location.go(this.urlSerializer.serialize(this.currentUrlTree));
this.routerEvents.next(new NavigationCancel(id, url)); this.routerEvents.next(new NavigationCancel(id, url));
return Promise.resolve(false); return Promise.resolve(false);
} }
@ -263,6 +264,7 @@ export class Router {
}) })
.forEach((shouldActivate) => { .forEach((shouldActivate) => {
if (!shouldActivate || id !== this.navigationId) { if (!shouldActivate || id !== this.navigationId) {
this.location.go(this.urlSerializer.serialize(this.currentUrlTree));
this.routerEvents.next(new NavigationCancel(id, url)); this.routerEvents.next(new NavigationCancel(id, url));
return Promise.resolve(false); return Promise.resolve(false);
} }
@ -355,7 +357,7 @@ class GuardChecks {
if (outlet && outlet.isActivated) { if (outlet && outlet.isActivated) {
forEach( forEach(
outlet.outletMap._outlets, outlet.outletMap._outlets,
(v, k) => this.deactivateOutletAndItChildren(v, outlet.outletMap._outlets[k])); (v, k) => this.deactivateOutletAndItChildren(v.activatedRoute.snapshot, v));
this.checks.push(new CanDeactivate(outlet.component, route)); this.checks.push(new CanDeactivate(outlet.component, route));
} }
} }
@ -447,7 +449,7 @@ class ActivateRoutes {
{provide: ActivatedRoute, useValue: future}, {provide: ActivatedRoute, useValue: future},
{provide: RouterOutletMap, useValue: outletMap} {provide: RouterOutletMap, useValue: outletMap}
]); ]);
outlet.activate(future._futureSnapshot._resolvedComponentFactory, resolved, outletMap); outlet.activate(future._futureSnapshot._resolvedComponentFactory, future, resolved, outletMap);
advanceActivatedRoute(future); advanceActivatedRoute(future);
} }