eventbus如何在Angular5中使用-創(chuàng)新互聯(lián)

這篇文章將為大家詳細(xì)講解有關(guān)eventbus如何在Angular5中使用,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

創(chuàng)新互聯(lián)公司成立于2013年,我們提供高端網(wǎng)站建設(shè)成都網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)、網(wǎng)站定制、成都營(yíng)銷網(wǎng)站建設(shè)、小程序設(shè)計(jì)、微信公眾號(hào)開(kāi)發(fā)、成都網(wǎng)站營(yíng)銷服務(wù),提供專業(yè)營(yíng)銷思路、內(nèi)容策劃、視覺(jué)設(shè)計(jì)、程序開(kāi)發(fā)來(lái)完成項(xiàng)目落地,為成都OPP膠袋企業(yè)提供源源不斷的流量和訂單咨詢。

1.package.json中

"dependencies": {
   ...
  "vertx3-eventbus-client": "3.5.2",
 },

然后  npm install,或者:

npm install vertx3-eventbus-client@3.5.2

2.angular-cli.json中

 "scripts": [
     ...
    "../node_modules/vertx3-eventbus-client/vertx-eventbus.js"
   ],

3.創(chuàng)建一個(gè)eventbus.service.ts用來(lái)通信

導(dǎo)入eventbus:

import { EventBus } from 'vertx3-eventbus-client';

聲明eventbus:

declare var EventBus: any;

4.創(chuàng)建eventbus實(shí)例,監(jiān)聽(tīng)接口以及發(fā)送消息

//創(chuàng)建實(shí)例
var eb = new EventBus('http://localhost:8080/eventbus');

eb.onopen = function() {
 //注冊(cè)監(jiān)聽(tīng)器用來(lái)接受消息
 eb.registerHandler('some-address', function(error, message) {
  console.log('received a message: ' + JSON.stringify(message));
 });

 //發(fā)送消息
 eb.send('some-address', {name: 'tim', age: 587});

}

更多信息請(qǐng)參考這里 https://vertx.io/docs/vertx-web/java/

注:

對(duì)于需要發(fā)送消息來(lái)接受的消息,需要先監(jiān)聽(tīng),然后再發(fā)送消息。
對(duì)于一直推送的消息,不需要發(fā)送。

代碼實(shí)例如下:

RegisterHandler(key, id, callback) {
    const address = '***.' + key + '.' + id;
    if (typeof (this.eventBus[key]) === 'undefined' || !this.eventBus[key]) {
      this.eventBus[key] = new EventBus(environment.eventbusUrl);
    }
    if (this.eventBus[key].state === EventBus.OPEN) {
      this.eventBus[key].registerHandler(address, callback);
    } else {
      const $this = this;
      this.eventBus[key].onopen = function () {
        $this.eventBus[key].registerHandler(address, callback)
      }
    }
  }

Send(key, id) {
    var data = '';
    const address = ***.' + key + '.' + id;
    if (typeof (this.eventBus[key]) === 'undefined' || !this.eventBus[key]) {
      this.eventBus[key] = new EventBus(environment.eventbusUrl);
    }
    if (this.eventBus[key].state === EventBus.OPEN) {
      this.eventBus[key].send(address, data)
    } else {
      const $this = this;
      this.eventBus[key].onopen = function () {
        $this.eventBus[key].send(address, data)
      }
    }
  }

closeEventBus(key) {
    if (typeof (this.eventBus[key]) !== 'undefined' && this.eventBus[key] && this.eventBus[key].state === EventBus.OPEN) {
      this.eventBus[key].close();
    }
    this.eventBus[key] = null;
}

關(guān)于eventbus如何在Angular5中使用就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

分享題目:eventbus如何在Angular5中使用-創(chuàng)新互聯(lián)
鏈接地址:http://muchs.cn/article26/eigjg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、網(wǎng)站建設(shè)、響應(yīng)式網(wǎng)站、電子商務(wù)、定制開(kāi)發(fā)、Google

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)