问题
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:
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)
}
来源:https://stackoverflow.com/questions/47934718/angular-4-service-cannot-read-property-newservice-of-undefined