Search code examples
ionic4

ion slide is not working when i navigate back in ionic 4


Auto Play Slider is Not Working

I tried all solutions from the given stack overflow nothing works for me I am using ionic4 slide

I am trying this from last 6 days not getting the solution please let me know proper solution so that I can fix this issue

 @ViewChild('sliderRef', { static:false }) slides: IonSlides;
 option = {
    slidesPerView: 1,
    spaceBetween: 10,
    centeredSlides: true,
     speed:1000,
     loop:true,
     autoplay:{disableOnInteraction:false}
  }
  ionViewWillEnter(){
  this.sliderThree =
      {
       
        slidesItems: [
          {
            id: "../../assets/images/market.jpg",
            name:"Sumo"
          },
          {
            id: "../../assets/images/mobile.jpg",
            name:"Shelcal 500"
          },
          {
            id: "../../assets/images/test.jpg",
            name:"Orofer Xt"
          },
        ]
      };
      this.slide.update();
      }
 async slideChange() {
     console.log("method called");
    
    this.slides.update();
  
  }
<ion-slides [options]="option" #sliderRef (ionSlideDidChange)="slideChange()">
          <ion-slide *ngFor="let item of sliderThree.slidesItems" style="width:90%;height:20vh;" (click)="labTest()">
              <ion-card style="width:90%;height:20vh;
              display:-webkit-box;
              overflow:hidden;border:1px solid grey;"  >
                  <img [src]="item.id" style="width:100%;height:20vh;
                  display:-webkit-box;" alt="">
             </ion-card>
          </ion-slide>
        </ion-slides>


Solution

  • Had an similar issue try manually starting on ionViewDidEnter and stoping it on ionViewDidEnter.

    But first remove your loop, and autoplay properties from the options like so:

     option = {
        slidesPerView: 1,
        spaceBetween: 10,
        centeredSlides: true,
        speed:1000
      }
    

    Then handle start and stop like this:

    ionViewDidEnter(){
       this.slide.startAutoplay();
    }
    
    ionViewDidLeave(){
       this.slide.stopAutoplay();
    }
    

    EDIT:

    If you want to make it work between tabs, then use the autoplay option like so:

     option = {
        slidesPerView: 1,
        spaceBetween: 10,
        centeredSlides: true,
        speed:1000,
        autoplay: {
           delay: 1000,
           disableOnInteraction: false
        }
      }