您可以使用如下方法:
move(shift: number, componentRef: ComponentRef<any>) {
const currentIndex = this.vcRef.indexOf(componentRef.hostView);
const len = this.vcRef.length;
let destinationIndex = currentIndex + shift;
if (destinationIndex === len) {
destinationIndex = 0;
}
if (destinationIndex === -1) {
destinationIndex = len - 1;
}
this.vcRef.move(componentRef.hostView, destinationIndex);
}
将移动组件,具体取决于
shift
值:
move(1, componentRef) - up
move(-1, componentRef) - down
Stackblitz example