首页 > 解决方案 > vue.js UI 单文件组件中的订阅者

问题描述

我正在尝试使用在 vue.js 中实现订阅者rabbitmq-pubsub

然而,通过做

var Subscriber = require('rabbitmq-pubsub').Subscriber;

在终端我看到

warning  in ./node_modules/_colors@1.0.3@colors/lib/colors.js

127:29-43 Critical dependency: the request of a dependency is an expression

在浏览器控制台中是 ee

"[WDS] Warnings while compiling."
"./node_modules/_colors@1.0.3@colors/lib/colors.js
127:29-43 Critical dependency: the request of a dependency is an expression
 @ ./node_modules/_colors@1.0.3@colors/lib/colors.js
 @ ./node_modules/_colors@1.0.3@colors/safe.js
 @ ./node_modules/_winston@1.1.2@winston/lib/winston/config.js
 @ ./node_modules/_winston@1.1.2@winston/lib/winston.js
 @ ./node_modules/_logfilename@0.1.3@logfilename/src/logfilename.js
 @ ./node_modules/rabbitmq-pubsub/build/subscriber.js
 @ ./node_modules/rabbitmq-pubsub/build/index.js
 @ ./node_modules/babel-loader/lib!./node_modules/vue-loader/lib/selector.js?type=script&index=0!./src/components/SelectServices.vue
 @ ./src/components/SelectServices.vue
 @ ./src/main.js
 @ multi (webpack)-dev-server/client?http://10.9.1.147:8081 webpack/hot/dev-server ./src/main.js"

但是当我这样做时:

var subscriberOptions = {
  exchange: 'service_deployment_exchange',
  queueName: queue,
  url: amqpl_url
};

var subscriber = new Subscriber(subscriberOptions);
function onIncomingMessage(message) {
  debug('onIncomingMessage ', message.fields);
  console.log(message);

  subscriber.ack(message);

  //subscriber.nack(message);
};

但是当我在我的 vue 单个文件组件的脚本元素中的挂载函数中调用以下行时

subscriber.start(onIncomingMessage)

我明白了

TypeError: fs.readdirSync is not a function

除了警告仍然出现在控制台中。

标签: javascriptnode.jsvue.js

解决方案


您不能在浏览器代码中使用节点的 api。而且我认为 rabbitmq-pubsub 是一个仅限节点的模块。


推荐阅读