我有这个范围的问题。我知道"this.catVotes = catData“行在另一个函数中,所以我不能将它直接发送到"catVotes: Number”;
我怎么才能解决这个问题?
catVotes: Number;
dogVotes: Number;
constructor(private votingService: VotingService, private socket: Socket) { }
ngOnInit() {
this.socket.on('catvotes', function(catData){
console.log(catData);
this.catVotes = catData;
});
this.socket.on('dogvotes', function(dogData){
console.log(dogData);
this.dogVotes = dogData;
});
}发布于 2018-12-29 00:06:24
您可以从简单函数更改为箭头函数。然后,this仍然引用您的类。
this.socket.on('catvotes', (catData) => {
console.log(catData);
this.catVotes = catData;
});但也可以查看JB提供的链接,因为它提供了一个更全面的答案,并对主题和选项进行了详细的讨论。
https://stackoverflow.com/questions/53965572
复制相似问题