angular - PubNub Angular2 AddListener - 调用函数

标签 angular pubnub

我卡在了 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/

相关文章:

angular - 如何在我的 Angular 12 应用程序中实现 firebase app-check

Angular 2 服务错误。找不到原因

ios - 当应用程序进入后台状态 iOS 8 时,Pubnub 连接停止

android - pubnub.subscribe 没有响应

ios - 防止在您发布到 PubNub 时向您发送推送通知

ios - APNS2 错误 400 : DeviceTokenNotForTopic - Getting this error when sending Push Notification via PubNub

node.js - Chartjs 在 Angular2 应用程序中的使用

javascript - Angular 2 HTTP获取处理404错误

angular - FullCalendar::Angular 5 应用程序中的日历问题

ios - 除非我先发布消息,否则无法接收 PubNub 消息