refactor(router): cleanup, simplification
This commit is contained in:

committed by
Matias Niemelä

parent
712630ca65
commit
44d48d9d7a
@ -37,8 +37,9 @@ import {PRIMARY_OUTLET} from '../shared';
|
||||
*/
|
||||
@Directive({selector: 'router-outlet'})
|
||||
export class RouterOutlet implements OnDestroy {
|
||||
private activated: ComponentRef<any>;
|
||||
private _activatedRoute: ActivatedRoute;
|
||||
private activated: ComponentRef<any>|null = null;
|
||||
private _activatedRoute: ActivatedRoute|null = null;
|
||||
private _outletName: string;
|
||||
public outletMap: RouterOutletMap;
|
||||
|
||||
@Output('activate') activateEvents = new EventEmitter<any>();
|
||||
@ -46,11 +47,12 @@ export class RouterOutlet implements OnDestroy {
|
||||
|
||||
constructor(
|
||||
private parentOutletMap: RouterOutletMap, private location: ViewContainerRef,
|
||||
private resolver: ComponentFactoryResolver, @Attribute('name') private name: string) {
|
||||
parentOutletMap.registerOutlet(name ? name : PRIMARY_OUTLET, this);
|
||||
private resolver: ComponentFactoryResolver, @Attribute('name') name: string) {
|
||||
this._outletName = name || PRIMARY_OUTLET;
|
||||
parentOutletMap.registerOutlet(this._outletName, this);
|
||||
}
|
||||
|
||||
ngOnDestroy(): void { this.parentOutletMap.removeOutlet(this.name ? this.name : PRIMARY_OUTLET); }
|
||||
ngOnDestroy(): void { this.parentOutletMap.removeOutlet(this._outletName); }
|
||||
|
||||
/** @deprecated since v4 **/
|
||||
get locationInjector(): Injector { return this.location.injector; }
|
||||
@ -58,24 +60,32 @@ export class RouterOutlet implements OnDestroy {
|
||||
get locationFactoryResolver(): ComponentFactoryResolver { return this.resolver; }
|
||||
|
||||
get isActivated(): boolean { return !!this.activated; }
|
||||
|
||||
get component(): Object {
|
||||
if (!this.activated) throw new Error('Outlet is not activated');
|
||||
return this.activated.instance;
|
||||
}
|
||||
|
||||
get activatedRoute(): ActivatedRoute {
|
||||
if (!this.activated) throw new Error('Outlet is not activated');
|
||||
return this._activatedRoute;
|
||||
return this._activatedRoute as ActivatedRoute;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when the `RouteReuseStrategy` instructs to detach the subtree
|
||||
*/
|
||||
detach(): ComponentRef<any> {
|
||||
if (!this.activated) throw new Error('Outlet is not activated');
|
||||
this.location.detach();
|
||||
const r = this.activated;
|
||||
this.activated = null !;
|
||||
this._activatedRoute = null !;
|
||||
return r;
|
||||
const cmp = this.activated;
|
||||
this.activated = null;
|
||||
this._activatedRoute = null;
|
||||
return cmp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when the `RouteReuseStrategy` instructs to re-attach a previously detached subtree
|
||||
*/
|
||||
attach(ref: ComponentRef<any>, activatedRoute: ActivatedRoute) {
|
||||
this.activated = ref;
|
||||
this._activatedRoute = activatedRoute;
|
||||
@ -86,8 +96,8 @@ export class RouterOutlet implements OnDestroy {
|
||||
if (this.activated) {
|
||||
const c = this.component;
|
||||
this.activated.destroy();
|
||||
this.activated = null !;
|
||||
this._activatedRoute = null !;
|
||||
this.activated = null;
|
||||
this._activatedRoute = null;
|
||||
this.deactivateEvents.emit(c);
|
||||
}
|
||||
}
|
||||
@ -129,11 +139,11 @@ export class RouterOutlet implements OnDestroy {
|
||||
const component = <any>snapshot._routeConfig !.component;
|
||||
|
||||
resolver = resolver || this.resolver;
|
||||
const factory = resolver.resolveComponentFactory(component) !;
|
||||
const factory = resolver.resolveComponentFactory(component);
|
||||
|
||||
const injector = new OutletInjector(activatedRoute, outletMap, this.location.injector);
|
||||
|
||||
this.activated = this.location.createComponent(factory, this.location.length, injector, []);
|
||||
this.activated = this.location.createComponent(factory, this.location.length, injector);
|
||||
this.activated.changeDetectorRef.detectChanges();
|
||||
|
||||
this.activateEvents.emit(this.activated.instance);
|
||||
|
Reference in New Issue
Block a user