在我的 Ionic 5 应用程序中,我必须通过 ion-slides 水平显示我的自定义组件,并在循环中垂直重复每个滑块。
在代码中。
<ng-container *ngFor="let item of mainList; let i = index;">
<ion-slides pager="true" loop="true" [options]="slideOpts" (ionSlideDidChange)="slideChanged(i)" #slides>
<ion-slide *ngFor="let id of item.subList; let j = index;">
<app-cust-comp [input]="j"></app-cust-comp>
</ion-slide>
</ion-slides>
</ng-container>
以下方法获取活动幻灯片编号
@ViewChild('slides') slides: IonSlides;
async slideChanged(i: number){
const activeSlideNumber = await this.slides.getActiveIndex();
console.log(i+' '+slideNumber);
}
我的目标是为每个索引获取正确的活动幻灯片编号。滑块工作正常,activeSlideNumber
每次只有第一个(索引 0)滑块的值都是正确的。对于除第一个滑块之外的所有滑块,其值activeSlideNumber
始终是第一个(索引 0)滑块更改的最后一个值。因此,如果我将第一个(索引 0)滑块滑动 3 次,activeSlideNumber
索引 0 的值为 2。对于所有其他滑块,它将始终为 2。