6

我有以下用类型脚本编写的类。当我编译它时,它会出错说

"src\main\MqttClientWrapper.ts(24,2): error TS1068: Unexpected token. A construct or, method, accessor, or property was expected.".

下面是我的代码。

var mqtt :any = require('mqtt');

export interface IWillMessage {
  topic: string;
  payload: string;
  qos: number;
  retain: string;
}

export interface IMessageReceivedCallBack {
  onMessageReceived(message : string);
}

export interface IMqttOptions {
  clientId: string;
  keepAlive: number;
  clean: string;
  reconnectPeriod: string;
  will: IWillMessage;
}

export default class MqttClientWrapper {

 client : any;

constructor(url: string, mqttOptions : IMqttOptions, messageReceivedCallBack : IMessageReceivedCallBack) {
   client = mqtt.connect(url, mqttOptions);
   client.on('message',function(topic : string, message : string){
     messageReceivedCallBack.onMessageReceived(message);
   }
}

subscribeMessage(topic : string) {
  client.subscribe(topic);
}

publishMessage(topic : string, message : string, level : number ) {
  client.publish(topic,message,level);
}

}

错误指向该行,

 client : any;

我也试过“var client:any;”和“let client:any”。我仍然得到同样的错误。在跟踪中还发现了以下错误行。

src\main\MqttClientWrapper.ts(26,16): error TS1005: ',' expected.
[16:44:41] [Typescript] TypeScript error: src\main\MqttClientWrapper.ts(26,16):
error TS1005: ',' expected.
src\main\MqttClientWrapper.ts(26,38): error TS1005: ',' expected.
[16:44:41] [Typescript] TypeScript error: src\main\MqttClientWrapper.ts(26,38):
error TS1005: ',' expected.
src\main\MqttClientWrapper.ts(26,78): error TS1005: ',' expected.
[16:44:41] [Typescript] TypeScript error: src\main\MqttClientWrapper.ts(26,78):
error TS1005: ',' expected.
src\main\MqttClientWrapper.ts(26,106): error TS1005: ';' expected.
[16:44:41] [Typescript] TypeScript error: src\main\MqttClientWrapper.ts(26,106):
 error TS1005: ';' expected.
src\main\MqttClientWrapper.ts(31,1): error TS1005: ',' expected.
[16:44:41] [Typescript] TypeScript error: src\main\MqttClientWrapper.ts(31,1): e
rror TS1005: ',' expected.
src\main\MqttClientWrapper.ts(33,24): error TS1005: ',' expected.
[16:44:41] [Typescript] TypeScript error: src\main\MqttClientWrapper.ts(33,24):
error TS1005: ',' expected.
src\main\MqttClientWrapper.ts(33,34): error TS1005: ';' expected.
[16:44:41] [Typescript] TypeScript error: src\main\MqttClientWrapper.ts(33,34):
error TS1005: ';' expected.
src\main\MqttClientWrapper.ts(37,22): error TS1005: ',' expected.
[16:44:41] [Typescript] TypeScript error: src\main\MqttClientWrapper.ts(37,22):
error TS1005: ',' expected.
src\main\MqttClientWrapper.ts(37,40): error TS1005: ',' expected.
[16:44:41] [Typescript] TypeScript error: src\main\MqttClientWrapper.ts(37,40):
error TS1005: ',' expected.
src\main\MqttClientWrapper.ts(37,56): error TS1005: ',' expected.
[16:44:41] [Typescript] TypeScript error: src\main\MqttClientWrapper.ts(37,56):
error TS1005: ',' expected.
src\main\MqttClientWrapper.ts(37,67): error TS1005: ';' expected.
[16:44:41] [Typescript] TypeScript error: src\main\MqttClientWrapper.ts(37,67):
error TS1005: ';' expected.
src\main\MqttClientWrapper.ts(41,1): error TS1128: Declaration or statement expe
cted.

下面是带有行号的代码。

在此处输入图像描述

我在这里做错了什么?这些错误是否也因为第一个“意外令牌错误”或这些行中的错误而出现?请指教。

4

1 回答 1

12

您不能使用letor定义类成员var,您只能使用publicorprivate或不使用 (or static)。

所以这就是导致你的问题的原因:

export default class MqttClientWrapper {
    let client : any; // <- right here

    // ...
}

这仅适用于您附加的图像,而不是您发布的代码。

于 2016-05-16T11:51:09.557 回答