問題があります。私はtypescriptでangular2にmqtt.jsライブラリを実装しようとしています。ここに私のapp.component.tsです:Angular2 with Typescript - mqtt.jsでクライアントが見つかりません
import { Component } from '@angular/core';
import * as mqtt from 'mqtt';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
private client: mqtt.Client;
ngOnInit(): void {
this.connectToMQTTBroker();
}
public connectToMQTTBroker() {
console.log('trying to connect');
this.client = mqtt.connect('ws://mqttBroker.de:1882');
this.client.addListener('connect', this.on_connect);
this.client.addListener('message', this.on_message);
}
private on_connect() {
console.log('connected');
this.client.subscribe('/broker/response/clientID');
this.client.publish('/providers', '{...Message...}');
}
private on_message (args: any[]) {
console.log(args[0]);
console.log(args[1]);
}
}
それは正常に接続され、私はログに「接続」というメッセージが表示さが、クライアント変数はon_connect方法でアクセスすることはできませんなぜ、私がCannot read property 'subscribe' of undefined
を取得?私はいくつかの基本的なTypescriptのものを紛失していると確信していますが、私は何が分かりません。