我使用ComponentFactoryResolver
来根据文档中所示的方式动态创建组件。
// create a component each time this code is run
public buildComponent() {
const factory = this.componentFactoryResolver.resolveComponentFactory(MyComponent);
const componentRef = this.viewContainerRef.createComponent(factory);
const component = componentRef.instance;
this.componentArray.push(component);
}
这段代码可以正常使用。每次函数运行时,都会创建一个新的MyComponent
并将其添加到提供的ViewContainerRef
位置的DOM中。现在,在用户执行某些操作后,我想重新排列组件。例如,我可能希望将最后创建的组件向上移动一个位置。是否可以在Angular内完成此操作?
public moveComponentUp(component) {
// What to do here? The method could also be passed the componentRef
}