离子3动态图像滑块在手动滑动后停止自动播放

时间:2017-12-14 17:10:00

标签: angular typescript ionic-framework ionic3

我从提供者rest api获取图像滑块数据。 Image Slider适用于自动播放,但在手动滑动后,图像滑块自动播放停止。当使用ionViewDidEnter()时,会生成错误...

我的.ts文件如下

    import {Slides } from 'ionic-angular';
    .
    .
    .
    export class DemoPage {
    .
    .
    .
    @ViewChild(Slides) slides: Slides; 
    .
    .
    .
    allslider: any;
    .
    .
    constructor(public navCtrl: NavController, public navParams: NavParams,public popoverCtrl: PopoverController,public demoapiService: DemoApiProvider)   
    {    
    this.imgSlider();    
    } 

    imgSlider() { 
    this.demoapiService.getImgSlider()  
    .then(myslide => { 
    this.allslider= myslide ;   
    });  
    }

    ionViewDidEnter() 
    {
   // this.slides.autoplayDisableOnInteraction = false;
   // when used above statement then error is generated
    }

我的.html文件如下所示

    <ion-slides class="slide-css"   #slides *ngIf="allslider && allslider.length" [autoplay]="2000" [speed]="500" [loop]="true" pager>
    <ion-slide   *ngFor="let slide of allslider ">            
    <img src="http://localhost:8000/{{slide.img}}" />
    </ion-slide>
    </ion-slides>

参考 - ionic 3 image slider stops autoplay after manual sliding

1 个答案:

答案 0 :(得分:1)

您无法在constructor()内使用它,因为尚未完全创建DOM

因此您需要删除this.imgSlider();

 constructor()   
  {    
     //this.imgSlider();<--- need to remove this and use it inside the `ionViewDidEnter()` event.
  }

像这样:

 ionViewDidEnter() 
    {
      this.imgSlider();
    }

 imgSlider() { 
    this.demoapiService.getImgSlider()  
    .then(myslide => { 
       this.allslider= myslide ;   
       this.slides.autoplayDisableOnInteraction = false;
      });  
    }