这个问题在这里已有答案:
我在newService中调用一个方法,在那个方法中我想调用方法changeMessage。问题是在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)
}
您需要使用箭头函数,此属性不会被覆盖,仍然引用早期的实例。
getContentReplies(parentAuthor, parentPermlink){
steem.api.getContentReplies(parentAuthor, parentPermlink) => ((result) {
this.newService.changeMessage(result)
}