angular - 如何检测 Swiper 中幻灯片的点击?

标签 angular typescript ionic3 swiper.js

我有一个 Angular 2 组件,其中包含 Swiper 包中的 slider 。我想知道我点击了哪张幻灯片(其索引)。尝试关注Swiper documentation我有这个:

import { Component, AfterViewInit } from "@angular/core";
import Swiper from "swiper";

@Component({
    selector: "challenges",
    templateUrl: "challenges.component.html"
})
export class ChallengesComponent implements AfterViewInit {
    public mySwiper: Swiper;
    public slides = [
        "https://via.placeholder.com/300x200/",
        "https://via.placeholder.com/300x200/",
        "https://via.placeholder.com/300x200/"
    ];

    constructor() { }

    public ngAfterViewInit() {
        this.mySwiper = new Swiper(".swiper-container", {
            slidesPerView: 3,
            spaceBetween: 30,
            pagination: {
                el: ".swiper-pagination",
                type: "bullets",
                clickable: true
            },
            on: {
                click: function(){
                    console.log(this.mySwiper.clickedSlide);
                }
        }
        });
    }
}

问题是,如果我单击一张幻灯片,则会出现此错误 this.mySwiper is undefined。如果 this.mySwiper 是类成员,为什么?

最佳答案

this 处于错误的上下文中。在文档中它说:

Please note, that this keyword within event handler always points to Swiper instance

尝试不使用 mySwiper,仅使用 this.clickedSlide

关于angular - 如何检测 Swiper 中幻灯片的点击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51501442/

相关文章:

ios - Ionic - iPhone X 状态栏覆盖共享标题组件

angular - 如何在不丢失准备好的 css 的情况下在 Ionic 2/3 中编写自定义组件?

angular - Mat-Autocomplete formControlName

带有 Firestore : delete specific fields from a document 的 Angularfire2

javascript - Angular 4组件被选中的复选框

angular - 在我的 angular 项目中实现 webassembly

node.js - 类型 'body' 上不存在属性 'Request'

html - 无法访问元素 margin-left

javascript - 使用 Material Angular 和 Leaflet 切换 sidenav 时调整 sidenav-content 的大小

ios - 如何解决应用程序验证 "The app references non-public selectors in Payload/MyApp.app/MyApp: _setAlwaysRunsAtForegroundPriority:"?