繁体   English   中英

当我从我的服务调用函数时,'this' 未定义

[英]'this' is undefined when I call a function from my service

我的component page上有这个调用

this.timerSessionService.startTimer(this.finish.bind(this), this.onSynchronice);

那么,它与伟大工程this.finish ,但是当我打电话onSynchronice从我的Service仍正常工作,并进入我的component时,我对这个问题涉及component我尝试调用的函数是在我的组件使用的.this ,它说:

错误错误:未捕获(承诺):类型错误:无法设置未定义的属性“pew”

这是因为我的onSynchronice功能我用.this ,然后它说,它不存在......我该如何解决?

在我的组件上

onSynchronice(pew) {
  console.log(pew["pew"]); //Works perfect
  this.pew= pew; //Crashes because of .this
  this.anyfunction(pew); //It crashes and says the same, anyfunction() does not exist, even if it exists
}

您可以将onSynchronice定义为箭头函数:

onSynchronice = (pew) => {
    console.log(pew["pew"]);
    this.pew= pew;
    this.anyfunction(pew);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM