Slides ViewChild

Hello, my ViewChilds slides are not working and I do not know the reason:


  @ViewChild('slidePregacoes') slidePregacoes: Slides;
  @ViewChild('slideEventos') slideEventos: Slides;

  proximo() {


            <ion-slides [options]="slideOptsEventos" #slideEventos>
            <ion-slides [options]="slideOptsPregacoes"  #slidePregacoes>

Anyone have any idea why it did not work?

When is proximo() being called? If the answer is not “definitively after the view has been completely built”, that’s your problem.

It is being called on a button after loading the entire page.

<ion-button size="small" color="tertiary" (click)="proximo()">

It works when I only have a Slider with the following code:

@ViewChild(Slides) slideEventos: Slides;

If I put more than one ‘slides’ and refer to it as @ViewChild (‘slidePregacoes’) it stops working.

Tricky one, first I think with v4, I mean you are trying with Ionic v4 right (?), you need to access them thru the nativeElement of ElementRef

@ViewChild('slideEventos') slideEventos: ElementRef;


Then, another tricky one, it’s depending of your configuration, slideNext might not work/be allowed, that’s the definition of the Swiper (not Ionic). If it still doesn’t work, could you plz show your options values?

Your suggestion solved. Thank you!
Now another error continues when page mute:

Uncaught TypeError: Can not read property 'removeEventListener' of undefined

Do you have any idea what that is? I know it’s also attached to the slides.

Update to beta.2 might help maybe?

Yesterday @brandyshea answered a question on the Ionic repo and mentioned that it’s better to access to Slides not with the native element but like following for example

@ViewChild('mySlider', { read: Slides }) slides: Slides;


It’s cleaner. Unfortunately it didn’t work that well and I gonna stick to native element, but maybe for you it could workout


I did the test and in my case it did not work either. Only with ElementRef that worked well. But, the ‘removeEventListener’ error remains.
Is the swiper you suggested to upgrade to beta.2?

In any case I would definitely suggest to update to beta.2 if you didn’t did yet, it’s better I think for the swiper and scroll

I think you are facing which is solved now

1 Like

All settled now. I changed the ElementRef as suggested and changed the nesting of my pages. Now everything running without errors. Thank you so much!

1 Like

Just for the record, in Ionic v4 today, the correct way to to this is:

import {
} from '@ionic/angular';
@ViewChild('slides', { read: IonSlides }) slides: IonSlides;
// moving to some slide index, just an example
this.slides.slideTo(3, 0);