angular - Angular:如何使用继承来扩展动态组件?
问题描述
我用这样的实例创建了一个动态组件,组件的名称是dynamicComponent1
:
createComponent() {
const componentType = this.getComponentType(this.type);
const factory = this.componentFactoryResolver.resolveComponentFactory(componentType);
this.componentRef = this.container.createComponent(factory);
const instance = <Data>this.componentRef.instance;
instance.data = this.data
}
像这样的课程:
export class Data{ data: any }
让我们假设这些数据有一个 ID。所以我尝试在下一个组件中扩展数据类并且是 DynamicComponent1 的一个实例。这个组件也创建了动态组件。我尝试从this.data更改实例ID。然后它的改变不是 instance.data.id 而是完整的数据被改变。
来自 DynamicComponent2 的代码:
@ViewChildren() 多个是 ViewContainerRef 的 QueryList
所以我们假设 data 的长度为 2 并且 data.id 是 {list,test} 并且倍数长度太 2
for (let i = 0; i < this.multiple.toArray().length; i++) {
const target = this.multiple.toArray()[i];
for (let j = 0; j < this.data.length; j++) {
const componentType = this.getComponentType(randomName + '_' + randomName);
if (componentType) {
const factory = this.componentFactoryResolver.resolveComponentFactory(componentType);
const cmpRef = target.createComponent(factory);
const instance = cmpRef.instance as Data;
this.logger.log('info', cmpRef.instance); // the first data.id is list0011 the second data.id is test0011
instance.data.id = data.id + i;
this.logger.log('info', cmpRef.instance); // data.id is list0011
this.logger.log('info', instance.data.id); // is list0 change after the loop to list0011 and test0011
this.logger.log('info', data.id); // is list0011
}
}
}
所以我的问题是,如何在不更改完整 data.id 的情况下更改此 instance.data.id?为什么 cmpRef.instance 在我初始化它们之前就已经知道 instance.data.id 了?我不知道为什么,它的行为如此令人毛骨悚然。我真的希望你能帮助我,我真的完成了。抱歉这段代码,我无法发送原始代码,所以我必须重写它。
如果您需要更多,我会尽力解释得更好。
我在尝试什么?
- 交接服务
- 创建一个具有 data.id 的 const
- 尝试在 instance.data.id 之后更改 data.id
解决方案
推荐阅读
- python - Whats wrong with this simple python code?
- python - django 模型数据库模式
- python - Firebase(客户端与服务器端)
- android - Xamarin 形成 PCL 浮动按钮和可扩展视图
- python - WS_TRANS = {ord(_wschar) : ' ' for _wschar in string.whitespace} AttributeError: 模块 'string' 没有属性 'whitespace'
- javascript - 如何对 ORM (sequelizejs) 查询添加条件检查?
- asp.net - 为什么我一直让您与此网站的连接不是私密的
- python - 将数据框隔离到另一个数据框
- json - Dialogflow:从 webhook 向用户打印 JSON 响应
- tls1.2 - TLSv1.2 使用 IBM Java 1.6 收到命运警报:handshak_failure