当我从服务中调用函数时,“this”未定义

2024-04-22

我的电话上有这个电话component page

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

嗯,它与this.finish,但是当我打电话时onSynchronice从我的Service它仍然有效并转到我的component问题出现在我的component我尝试使用调用组件内部的函数.this, 它说:

错误错误:未捕获(承诺中):TypeError:无法设置未定义的属性“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);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

当我从服务中调用函数时,“this”未定义 的相关文章

随机推荐