【问题标题】:How to get the instance of a ion-slides如何获取离子幻灯片的实例
【发布时间】:2016-08-06 07:57:36
【问题描述】:

我正在使用 Ionic 2 ion-slides,并想在其上调用一些实例方法。我有以下标记:

<ion-content padding>
  <ion-slides (ionDidChange)="onSlideChanged($event)" id="loopSlider">
   <ion-slide *ngFor="let slide of slides">
    <h1>{{ slide.title }}</h1>
  </ion-slide>
</ion-slides>
</ion-content>

我已尝试使用在(可能已过时)示例中找到的以下代码:

 private gotoSlide(index: number): void {
   this.sliderComponent = this.app.getComponent('loopSlider');
   this.sliderComponent.slider.slideTo(index);
  }

this.app.getComponent('loopSlider'); 总是返回null

有谁知道如何获取组件实例以便我可以使用它的 API?

【问题讨论】:

    标签: angular ionic2


    【解决方案1】:

    根据the Ionic documentation,您可以使用@ViewChild 执行此操作:

    import { Component, ViewChild } from '@angular/core';
    import { Slides } from 'ionic-angular';
    ...
    
    @Component({
      ...
    })
    class MyPage {
      @ViewChild('loopSlider') sliderComponent: Slides;
    
      ...
    }
    

    【讨论】:

    • 非常感谢!它就在 doco 中(我错过了)
    • 对于仍然遇到问题的每个人:确保您确实已导入Slides
    【解决方案2】:

    现在 Ionic4 已经推出一段时间了,为它更新这样的答案可能很省钱。

    import { IonSlides } from '@ionic/angular';
    
    export class...
    @ViewChild(IonSlides) productSlider: IonSlides;
    

    【讨论】:

    • 我不敢相信他们甚至懒得更新他们的文档。非常感谢。
    猜你喜欢
    • 2018-08-17
    • 2015-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-12
    • 2019-11-18
    • 1970-01-01
    • 2018-12-09
    相关资源
    最近更新 更多