Issue with ion-Slides swipe

I want to disable swipes on my ion-slides.

home.html

  <ion-slides>
  <ion-slide>
    <h1>Slide 1</h1>
  </ion-slide>
  <ion-slide>
    <h1>Slide 2</h1>
  </ion-slide>
  <ion-slide>
    <h1>Slide 3</h1>
  </ion-slide>
</ion-slides>

home.ts

...

@ViewChild(Slides) slides: Slides;

....

  constructor(public navCtrl: NavController, public navParams: NavParams) {
    
  this.slides.lockSwipes(true);
    
  }
...

But this gives a lot of errors…can anyone tell what may be the issue?

ERRORS:
Uncaught (in promise): TypeError: Cannot read property ‘lockSwipes’ of undefined
TypeError: Cannot read property ‘lockSwipes’ of undefined
at new QuestionDetails…

Very much likely because you’re callingthis.slides.lockSwipes(true) from within your constructor. I guess the slides aren’t initalized at that moment yet, so the ViewChild is undefined at that moment in time.

Try running this.slides.lockSwipes(true) after your view has rendered, i.e. wrap it inside ionViewDidLoad() { this.slides.lockSwipes(true) } and place it outside your constructor.

3 Likes

Thanks man…!! :smile:
Works like charm

1 Like