首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么在将Paho MQTT函数从Angular 1迁移到Angular 2时收到typescript错误?

为什么在将Paho MQTT函数从Angular 1迁移到Angular 2时收到typescript错误?
EN

Stack Overflow用户
提问于 2017-03-11 00:17:53
回答 1查看 763关注 0票数 1

我正在尝试连接到我的MQTT代理,我已经在我的ionic 2,angular 2应用程序中创建了一个mqtt提供程序,该提供程序如下所示:

代码语言:javascript
复制
import { Component } from '@angular/core';
import { NavController, ViewController } from 'ionic-angular';
import { Observable } from 'rxjs/Observable';
import { Paho } from 'ng2-mqtt/mqttws31';



@Component({
  selector: 'page-greywater',
  templateUrl: 'greywater.html'
})
export class MQTT_Greenchain {
  private _client: Paho.MQTT.Client;
  private options = {
    userName: 'rdjghvoh',
    password: 'w7Ex0VTqZViw',
    timeout: 30,
    useSSL:true,
    onSuccess:this.onConnected,
  };
  private topic: string;
  public displayedMessage: string;
  public mes: Paho.MQTT.Message;
  public constructor() {
    this._client = new Paho.MQTT.Client(
      "m20.cloudmqtt.com",
      Number(30775),
      "",
      "peter"
    );
    this._client.onConnectionLost = (responseObject: {errorCode: Number, errorMessage: string}) => {
      console.log('poes');
      console.log(responseObject.errorMessage);
    };

    this._client.onMessageArrived = (message: Paho.MQTT.Message) => {
      this.onMessageArr(message);
      console.log('Message arrived.');
    };
    this.topic = "haha";
    this.displayedMessage = "what I was";
  }
  connectMe() {
    console.log("MQTT OPTIONS: " + this.options);
    this._client.connect(this.options);
  }
  private onConnected(): void {
    console.log('Connected to broker.');
    this._client.subscribe(this.topic);
    this.mes = new Paho.MQTT.Message("-1"); // -1 => Notify
    this.mes.destinationName = this.topic;
    this._client.send(this.mes);
  }
  private onMessageArr(message: Paho.MQTT.Message){
    this.displayedMessage = message.payloadString;
  }
}

我已经能够毫无困难地在angular 1中调用下面的代码,并且我能够让所有与MQTT相关的东西都正常工作。角度1中的函数如下:

代码语言:javascript
复制
function onConnect() {
  sharedUtils.hideLoading();
  console.log("onConnect, CURRENT TOPIC: " + mqttData);
  client.subscribe(mqttData.currentTopic);
}

在上面的代码中,mqttData.currentTopic仅仅是一个字符串。

该函数接受1个参数,即使它可以接受2个( options对象)。

在angular 2中,typescript给了我一个错误:

代码语言:javascript
复制
Supplied parameters do not match any signature of call target

为什么它不允许我像在angular 1中那样用一个参数调用函数?如果我给它{}作为第二个参数:

代码语言:javascript
复制
this._client.subscribe(this.topic, {});

我得到的错误是:

代码语言:javascript
复制
AMQJS0005E Internal error. Error Message: Cannot read property 'subscribe' of undefined, Stack trace: TypeError: Cannot read property 'subscribe' of undefined

这是在传递给onConnectionLost回调函数的response对象参数中收到的错误。

我非常确定我的'this._client‘不是未定义的,因为消息是’已连接到代理‘。出现在控制台中,表明明显调用了connect方法的onConnected、onSuccess属性回调?

我有什么没听懂的?

EN

回答 1

Stack Overflow用户

发布于 2018-12-04 23:47:28

试一试,如果工作正常,请回复

代码语言:javascript
复制
  this.client.subscribe(this.topic, '');
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42723018

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档