我卡在了 addListener 事件上。收到一条消息后,我想调用然后在我的代码中调用一个函数,但出现找不到函数的错误。
错误类型错误:this.plotBus 不是函数
一个非常简单的例子。
this.pubnub.publish({
channel: 'test',
message:["hello"]
})
this.pubnub.addListener({
message: function(msg) {
console.log(msg);
this.plotBus(msg)
}
})
this.pubnub.subscribe({
channels: ['test'],
triggerEvents: ['message']
});
plotBus(bus){
console.log("Plotting Bus with received data")
}
最佳答案
传统函数中的“this”并没有像您预期的那样工作。一个聪明的替代方法是改用箭头函数。
this.pubnub.addListener({
message: msg=> {
console.log(msg);
this.plotBus(msg)
}
})
关于angular - PubNub Angular2 AddListener - 调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50185320/