Ragesh Pikalmunde
Ragesh Pikalmunde

Reputation: 1403

TypeError: Cannot read property 'slideTo' of undefined

I am using ion-Slides to show the selected image from the gallery and swipe them horizontally and it is working good but I want to show the last selected image on the screen.

I tried using slideTo() and it causing the type error.

For more clarity: I am running a loop where I push selected images from gallery in with base64 conversion into an array and pass it in an image tag to show images on the HTML screen.

any help would be great.

Upvotes: 1

Views: 4735

Answers (2)

Ghonche Yqr
Ghonche Yqr

Reputation: 364

You can use ionSlidesDidLoad event to make sure ion-slide has loaded.

subscribe for the event in your html file like following sample:

<ion-content>
  <ion-slides  (ionSlidesDidLoad)="slidesLoaded($event)">
    <ion-slide>
      <h1>Slide 1</h1>
    </ion-slide>
    <ion-slide>
      <h1>Slide 2</h1>
    </ion-slide>
  </ion-slides>
</ion-content>

and in your ts file add the following method:

    public slidesLoaded($event) {
    //move to slide number 2
    $event.target.slideTo(2);
    }

This event is available in Ionic 4 and later.

Upvotes: 0

Mr. Disability
Mr. Disability

Reputation: 838

@ViewChild('slides', { static: false }) slider: IonSlides; I faced a similar problem. Had to change static from true to false.

Upvotes: 2

Related Questions