如何在angular 2 typescript app中使用(mqtt)js库?

时间:2016-05-03 13:05:16

标签: javascript typescript npm angular typing

我与how-to-use-moment-js-library-in-angular-2-typescript-app中采用的方法非常相似,但仍然得到private void btnEkle_Click(object sender, EventArgs e) { baglan.Open(); SqlCommand cmd = new SqlCommand("INSERT INTO uyeler (uye_adi,uye_soyadi,uye_yasi,uye_adresi,uye_telefonu,uye_mailadresi) values ('@uye_adi','@uye_soyadi','@uye_yasi','@uye_adresi','@uye_telefonu','@uye_mailadresi')",baglan); cmd.Connection = baglan; cmd.Parameters.AddWithValue("@uye_adi",textAd.Text); cmd.Parameters.AddWithValue("@uye_soyadi",textSoyad.Text); cmd.Parameters.AddWithValue("@uye_yasi",textYas.Text); cmd.Parameters.AddWithValue("@uye_adresi",textAdres.Text); cmd.Parameters.AddWithValue("@uye_telefonu",textTelefon.Text); cmd.Parameters.AddWithValue("@uye_mailadresi",textMail.Text); cmd.ExecuteNonQuery(); baglan.Close(); listele(); }

error TS2307: Cannot find module 'mqtt'.

没有找到打字但这确实......

npm install --save mqtt
<s>typings install --save mqtt</s

我的tsconfig.conf看起来像这样

typings install mqtt --save --ambient 

{ "compilerOptions": { "noImplicitAny": true, "module": "commonjs", "target": "ES5", "emitDecoratorMetadata": true, "experimentalDecorators": true, "sourceMap": true, "declaration": true }, "files": [ "ng2-mqtt.ts" ], "exclude": [ "node_modules" ] } 就是这个......

ng2-mqtt.ts

export * from './src/mqtt.service' 有..

./src/mqtt.service.ts

tsc -version 1.8.10,angular2 @2.0.0-beta.17,typings 0.8.1,npm 2.14.20,node v4.4.1,Windows 7

将Angular 2与其打字世界之外的元素一起使用会不会太难?

2 个答案:

答案 0 :(得分:2)

我做了以下工作让我的工作:

1)首先通过package.json在依赖项中安装ng2-mqtt并运行npm update,以便在node_modules

中使用它

2)在index.html中,请务必包含它:

<html>
<head>
    <title>whatever</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.css">
    <script>document.write('<base href="' + document.location + '" />');</script>
    <script src="node_modules/ng2-mqtt/mqttws31.js" type="text/javascript"></script>

3)将mqtts映射添加到systemjs.config.js(参见map):

System.config({
transpiler: 'typescript',
typescriptOptions: {emitDecoratorMetadata: true},
map: {
    '@angular': 'node_modules/@angular',
    'mqttws31': 'node_modules/ng2-mqtt/mqttws31.js'
},

4)像往常一样导入:

import {Paho} from 'mqttws31'

5)在@Component中使用它:

 this.client = new Paho.MQTT.Client("localhost", Number(61614), "myclientid_");

    this.client.onConnectionLost = (responseObject: Object) => {
        console.log('Connection lost.');
        this.connected ="false";
      };

    this.client.onMessageArrived = (message: Paho.MQTT.Message) => {
        console.log('Message arrived.');
        this.msg =message.payloadString;
    };

    this.client.connect({ onSuccess: this.onConnected.bind(this); });

如果一切顺利,你应该看到你的连接(假设activemq): img

请参阅此处了解用法: https://github.com/icanos/ng2-mqtt

答案 1 :(得分:0)

对我没有任何帮助。在我创建的角度8中,请Click Here获取该代码。希望它能起作用。