Typescript Konva class 变量不在范围内

Typescript Konva class variable not in scope

我有一个要在 Konva.js 上使用的 Ionic 项目。我遇到的问题是我的 class 方法之一没有在内部函数中被识别。我试图在用户单击一段文本 (tossUpText) 后调用一个函数,但我目前收到一个错误(在代码中指出),因为该函数显示为未定义。

这里是错误的片段:https://i.imgur.com/5tWyTHS.png

我做了更多分析,发现在那个范围内,thishttps://i.imgur.com/VJzjHKd.png

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import * as Konva from 'konva';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  tossUpState: string = 'NOT_STARTED';
  stage: Konva.Stage;
  public layer: Konva.Layer;
  public tossUpText: Konva.Text;

  constructor(public navCtrl: NavController) {

  }

  ngAfterViewInit() {
    var width = window.innerWidth;
    var height = window.innerHeight;

    this.stage = new Konva.Stage({
      container: 'container',
      width: width,
      height: height
    });

    this.layer = new Konva.Layer();

    this.tossUpText = new Konva.Text({
      x: this.stage.getWidth() / 2 - 100,
      y: 15,
      text: '5.00',
      fontSize: 100,
      fill: 'green'
    });
    this.tossUpText.align('center');


    // add the shape to the layer
    this.layer.add(this.tossUpText);

    // add the layer to the stage
    this.stage.add(this.layer);

    this.tossUpText.on('mouseup touchend', function(){
      this.startTossUpTimer(); // error here
    });

  }


  startTossUpTimer() {
    console.log(this);
    if (this.tossUpState == 'NOT_STARTED') {
      // this.setTossUpTime();
    }
  }

}

使用箭头函数保存this参考:

this.tossUpText.on('mouseup touchend', () => {
  this.startTossUpTimer(); // error here
});