【发布时间】:2021-04-30 01:34:07
【问题描述】:
假设我有一个 dynamic-component-wrapper 可以实例化任何传递给它的 Component 类。
// DRE013 DCOOKE 16/05/2017 - The component to instantiate.
@Input() componentType: Type<{}>;
// DRE013 DCOOKE 16/05/2017 - the component that will be created
private _cmpRef: ComponentRef<Component>;
// DRE013 DCOOKE 16/05/2017 - Creates a component ref in the view at #target
createComponent(){
let factory = this.componentFactoryResolver.resolveComponentFactory(this.componentType);
this._cmpRef = this.target.createComponent(factory);
//this._cmpRef.instance.inputs >>>>>>>>> this is a string[] and I do not see how I can use this
}
示例用法
<shared-dynamic-component [componentType]="TestComponent"></shared-dynamic-component>
在哪里
TestComponent = TestComponent //class
这按预期工作,我可以从dynamic-component-wrapper 中接收此组件的实例,如下所示:
this._cmpRef.instance
Angular docs 在此 instance 对象上并不清楚 - 只是说明该实例是 C 类型 - 绝对没有提及 C 的实际含义。
谢天谢地,我的 IDE 能够告诉我:
ComponentRef.instance 具有以下属性:
inputs : string[]outputs : string[]
但是我不明白我应该如何使用这些信息。我想这只是@Input 字段的名称 - 但我想不出如何将复杂对象作为输入传递。
问题
我是否可以在使用componentFactoryResolver 动态创建组件后设置@Inputs 和其他元数据?
【问题讨论】:
-
C是一个泛型类型参数,因为我猜这个类的其他地方都使用了 T。 -
我可以设置@Inputs - 你想让它们被Angular自动更新吗?
-
@Maximus 这将是理想的 - 我知道这听起来有点牵强。
标签: angular