从Angular 4中的类名解析组件

从Angular 4中的类名解析组件,angular,Angular,是否可以从字符串解析组件 在前面的Angular 2中,我们能够做到这一点。然而,在Angular 4中,相同的方法将抛出错误 private resolver: ComponentFactoryResolver var factories = Array.from(this.resolver['_factories'].keys()); var factoryClass = <Type<any>>factories.find((x: any) => x.name

是否可以从字符串解析组件

在前面的Angular 2中,我们能够做到这一点。然而,在Angular 4中,相同的方法将抛出错误

private resolver: ComponentFactoryResolver

var factories = Array.from(this.resolver['_factories'].keys());
var factoryClass = <Type<any>>factories.find((x: any) => x.name === this.comp);
const factory = this.resolver.resolveComponentFactory(factoryClass);
const compRef = this.vcRef.createComponent(factory);
专用解析器:ComponentFactoryResolver
var factories=Array.from(this.resolver[''u factories'].keys());
var factoryClass=factories.find((x:any)=>x.name==this.comp);
const factory=this.resolver.resolveComponentFactory(factoryClass);
const compRef=this.vcreef.createComponent(工厂);

看起来您可能依赖于实现细节。但是,为了避免错误,您可以显式地将类型强制转换为
any
,以便访问索引器。假设有一个
\u工厂
属性,这应该可以工作:

var factories = Array.from((<any>this.resolver)['_factories'].keys());
var factories=Array.from((this.resolver)[''u factories'].keys());

错误是什么?元素隐式地具有“any”类型,因为“ComponentFactoryResolver”没有索引签名