2016-03-02 61 views
9

在應用程序歡迎頁面/幻燈片上使用離子幻燈片組件(4張幻燈片)。我需要用戶跳到上一張幻燈片的能力。 Docs說離子幻燈片實現Swiper API。我需要訪問如下方法:mySwiper.slideTo(index, speed, runCallbacks);Ionic 2幻燈片組件 - 如何訪問Swiper API

有關如何實施的提示?

+0

我在找這個。 –

回答

6

您可以選擇屬性中傳遞一個函數。

Original answer

@Component({ 
    selector: 'my-component', 
    template: '<ion-slides [options]="options"> 
       <ion-slide>Slide1</ion-slide> 
       <ion-slide>Slide2</ion-slide> 
      </ion-slides>', 
    directive: [IONIC_DIRECTIVES] 
}) 
export class MyComponent { 
    public slider: any; 

    constructor() { 
    this.options = { 
     onlyExternal: false, 
     onInit: (slides: any) => 
     this.slider = slides 
    } 
    } 

    click() { 
    this.slider.sliderNext(true, 250); 
    } 
} 

如需更多選項,請參見swiper api

0

,您可以撥打服務與你的組隊,探索

@Injectable() 
export class HomeSwiper { 
    swiper = null; 

    initSwiper(selector) { 
    this.swiper = new Swiper(selector, { 
     pagination: '.home-swiper-pagination', 
     speed: 400, 
     spaceBetween: 100, 
     nextButton: '.swiper-button-next', 
     prevButton: '.swiper-button-prev' 
    }); 
    } 

    goTo(index) { 
    this.swiper.slideTo(index); 
    } 
} 

而且使用它到您的@Page

@Page({ 
    templateUrl: 'build/pages/home/home.html', 
    providers: [HomeSwiper] 
}) 
export class Home { 
    constructor(private swiperService: HomeSwiper) { 
    this.swiperService.initSwiper('.home-modal-swiper-container'); 
    } 

    skipSlides() { 
    this.swiperService.goTo(indexOfTheLastSlide); 
    } 
} 
+0

使用這種方法,這是否意味着我需要安裝獨立於Ionic 2 Slides實現的Swiper庫? –

1

如果您正在尋找無定製指令,一個簡單的解決方案,你可以試試這個

constructor(
    private _app: IonicApp 
){} 
    ngAfterViewInit() { 
    this._slider = this._app.getComponent('my-slider'); 
    } 
    goToSlide(slideIndex){ 
    this.slider.slider.slideTo(slideIndex); 
    }