feat(router): use componentFactoryResolver
This commit is contained in:
@ -6,7 +6,8 @@
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {Attribute, ComponentFactory, ComponentRef, Directive, ReflectiveInjector, ResolvedReflectiveProvider, ViewContainerRef} from '@angular/core';
|
||||
import {Attribute, ComponentFactory, ComponentFactoryResolver, ComponentRef, Directive, NoComponentFactoryError, ReflectiveInjector, ResolvedReflectiveProvider, ViewContainerRef} from '@angular/core';
|
||||
|
||||
import {RouterOutletMap} from '../router_outlet_map';
|
||||
import {ActivatedRoute} from '../router_state';
|
||||
import {PRIMARY_OUTLET} from '../shared';
|
||||
@ -22,7 +23,7 @@ export class RouterOutlet {
|
||||
*/
|
||||
constructor(
|
||||
parentOutletMap: RouterOutletMap, private location: ViewContainerRef,
|
||||
@Attribute('name') name: string) {
|
||||
private componentFactoryResolver: ComponentFactoryResolver, @Attribute('name') name: string) {
|
||||
parentOutletMap.registerOutlet(name ? name : PRIMARY_OUTLET, this);
|
||||
}
|
||||
|
||||
@ -44,10 +45,27 @@ export class RouterOutlet {
|
||||
}
|
||||
|
||||
activate(
|
||||
factory: ComponentFactory<any>, activatedRoute: ActivatedRoute,
|
||||
providers: ResolvedReflectiveProvider[], outletMap: RouterOutletMap): void {
|
||||
activatedRoute: ActivatedRoute, providers: ResolvedReflectiveProvider[],
|
||||
outletMap: RouterOutletMap): void {
|
||||
this.outletMap = outletMap;
|
||||
this._activatedRoute = activatedRoute;
|
||||
|
||||
const snapshot = activatedRoute._futureSnapshot;
|
||||
const component: any = <any>snapshot._routeConfig.component;
|
||||
|
||||
let factory;
|
||||
try {
|
||||
factory = typeof component === 'string' ?
|
||||
snapshot._resolvedComponentFactory :
|
||||
this.componentFactoryResolver.resolveComponentFactory(component);
|
||||
} catch (e) {
|
||||
if (!(e instanceof NoComponentFactoryError)) throw e;
|
||||
const componentName = component ? component.name : null;
|
||||
console.warn(
|
||||
`No component factory found for '${componentName}'. Add '${componentName}' to the 'precompile' list of your application component. This will be required in a future release of the router.`);
|
||||
factory = snapshot._resolvedComponentFactory;
|
||||
}
|
||||
|
||||
const inj = ReflectiveInjector.fromResolvedProviders(providers, this.location.parentInjector);
|
||||
this.activated = this.location.createComponent(factory, this.location.length, inj, []);
|
||||
}
|
||||
|
Reference in New Issue
Block a user