我想根据给定的json动态创建组件。第一层是TestComponent,它可以具有许多TestLayerComponent。问题是TestLayerComponent也可以具有许多TestLayerComponents。我使用它来动态地创建幻灯片推式菜单。如果Layer路径以Europe开头创建带有文本欧洲的
[{
Username:"",
Password:"",
Maps:[
{
Name:"",
Title:"",
Layers:[
id:""
path:"Europe/Balkans/Greece"
]
},
{
Name:"",
Title:"",
Layers:[
id:""
path:"Europe/Balkans/Romania"
]
},
{
Name:"",
Title:"",
Layers:[
id:""
path:"Asia/China"
]
},
]
}
]
这是我尝试的代码,但仍仅显示最后一个组件。
for (const map of maps) {
mapViewContainerRef.clear();
let mapRow = mapViewContainerRef.createComponent(mapFactory);
mapRow.instance.maps = map;
for (const layer of map.layers) {
mapFactory = this.menuFactory.resolveComponentFactory(TestLayerComponent);
mapViewContainerRef.clear();
mapViewContainerRef.createComponent(mapFactory);
mapRow = mapViewContainerRef.createComponent(mapFactory);
mapRow.instance.layers = layer;
}
mapRow.destroy();
}
for (const map of maps) {
mapViewContainerRef.clear();
let mapRow = mapViewContainerRef.createComponent(mapFactory);
let index = 1;
mapRow.instance.maps = map;
for (const layer of map.layers) {
mapFactory = this.menuFactory.resolveComponentFactory(TestLayerComponent);
mapRow = mapViewContainerRef.createComponent(mapFactory, index);
index++;
}
}