我试图创建某种@Component工厂,我调用一个返回组件的函数。我想将widgetName传递给构造函数或超级构造函数。如何将参数传递给构造函数?
export function createCommonKendoComponent(selector: string, **widgetName**: string) {
@Component({
selector: selector,
inputs: ['bound', 'role'],
bindings: [ElementRef]
})
@View({ template: '<ng-content></ng-content>' })
class CommonComponent extends KendoComponent {
constructor(elementRef) {
super(elementRef, **widgeteName**);
}
}
return CommonComponent;
}
答案 0 :(得分:1)
您可以将其添加为某个地方的提供商。由于函数没有可用作提供程序的类型,因此您需要使用令牌。令牌可以是字符串或OpaqueToken
var token = new OpaqueToken('myfunction');
bootstrap(AppComponent, [
provide(token,
{useValue: (selector: string, **widgetName**: string) => {
createCommonKendoComponent(selector, **widgetName**}})]);
class CommonComponent extends KendoComponent {
constructor(elementRef:ElementRef, @Inject(token) private compFactory) {
}
}