首页 > 解决方案 > 无法读取属性 ... undefine ,在 Object.callbackFromNative In angular 2

问题描述

我有一个按流的代码

declare var Speech: any;
    export class Lesson implements OnInit {
       text = '';
       constructor()
{
this.text = 'hh';
}
ToSpeech(){
  Speech.listen(this.listenSucess);
}
listenSucess(result){
this.text = reuslt;
}

文件 Speech.js

var Speech= (function () {
return {
   listen:funtion(sucess){
      window.plugins.speechRecognition.startListening(function (result) {
          success(result);
      }
   }
}

我有一个错误,例如:

Cannot read property 'this.text' of undefined 
at Object.callbackFromNative

我为此功能使用了回调,谢谢。

标签: javascriptangularcallback

解决方案


您是否尝试过胖箭头=>而不是function?因为this在 a 内部无法访问function

var Speech = (() => {
return {
   listen = (success) => {
      window.plugins.speechRecognition.startListening((result) => {
          success(result);
      }
   }
}

推荐阅读