angular 4服务无法读取未定义的属性'newService'[重复]

问题描述 投票:0回答:1

这个问题在这里已有答案:

我在newService中调用一个方法,在那个方法中我想调用方法changeMes​​sage。问题是在newService.getContentReplies中this.newService不引用NewService的实例,因此它无法读取它的属性。我能做些什么来完成这项工作?

component.ts

  newService.getContentReplies(this.parentAuthor, this.parentPermlink) 

newService.service.ts

 getContentReplies(parentAuthor, parentPermlink){
  steem.api.getContentReplies(parentAuthor, parentPermlink, function(err, 
   result) {
    this.newService.changeMessage(result)
  }
angular service undefined
1个回答
1
投票

您需要使用箭头函数,此属性不会被覆盖,仍然引用早期的实例。

 getContentReplies(parentAuthor, parentPermlink){
  steem.api.getContentReplies(parentAuthor, parentPermlink) => ((result) { 
    this.newService.changeMessage(result)
 }
© www.soinside.com 2019 - 2024. All rights reserved.