angular 4 service cannot read property 'newSer

2019-06-11 23:39发布

This question already has an answer here:

I am calling a method in newService and in that method I want to call the method changeMessage. The problem is that in newService.getContentReplies this.newService does not refer to an instance of NewService so it can't read the property of it. What can I do to make this work?

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)
  }

1条回答
Summer. ? 凉城
2楼-- · 2019-06-11 23:57

You need to use arrow function, the this property is not overwritten and still references the earlier instance.

 getContentReplies(parentAuthor, parentPermlink){
  steem.api.getContentReplies(parentAuthor, parentPermlink) => ((result) { 
    this.newService.changeMessage(result)
 }
查看更多
登录 后发表回答