Javascript can';t调用-this-insideYouTube api事件(onReady等)

Javascript can';t调用-this-insideYouTube api事件(onReady等),javascript,angular,typescript,ionic2,ionic3,Javascript,Angular,Typescript,Ionic2,Ionic3,我不能在我的函数中调用任何函数或变量 我有onReady事件(youtube api事件) 同样,当我执行console.log(这)时,结果在函数中是未定义的 这是我的打字脚本代码 isOn :boolean = false; createPlayer(): void { return new window['YT'].Player(this.youtube.playerId, { height: this.youtube.playerHeight, width: t

我不能在我的函数中调用任何函数或变量

我有onReady事件(youtube api事件)

同样,当我执行console.log(这)时,结果在函数中是未定义的

这是我的打字脚本代码

isOn :boolean =   false;

createPlayer(): void {
  return new window['YT'].Player(this.youtube.playerId, {
    height: this.youtube.playerHeight,
    width: this.youtube.playerWidth,
    playerVars: {
      rel: 0,
      showinfo: 0
    },
    events: {
      'onReady': this.onPlayerReady,
      'onStateChange': this.state,
    }
  });
}


public state(event) {
  this.isOn = true; //here is the problem and the result is not defined 
}
//这门课来自离子2


您应该像这样使用箭头函数

isOn :boolean =   false;

createPlayer(): void {
  return new window['YT'].Player(this.youtube.playerId, {
    height: this.youtube.playerHeight,
    width: this.youtube.playerWidth,
    playerVars: {
      rel: 0,
      showinfo: 0
    },
    events: {
      'onReady': () => { /* Put your logic here! */ }
      'onStateChange': () => { this.isOn = true; /* Now it should work! */ }
    }
  });
}
当使用常规函数时,this关键字引用函数本身,但当使用箭头函数时,this属性不会被覆盖,仍然引用组件实例(其中定义了navCtrl属性)