[英]Node.js EventEmitter `on` event is not responding to `emit` actions
I have a Node.js app that represents a class room. 我有一个代表教室的Node.js应用程序。 I'm using Node version 11.11.0.
我正在使用Node版本11.11.0。 This app has two files: index.js and student.js.
这个程序有两个文件:index.js和student.js。 The relevant code looks like this:
相关代码如下:
index.js index.js
const EventEmitter = require('events');
const Student = require('./student');
async function start() {
eventEmitter.on('newListener', (event, listener) => {
console.log(`Added ${event} listener.`);
});
eventEmitter.on('handRaised', (question) => {
console.log(question);
});
for (let i=0; i<10; i++) {
let student = new Student(`Student #${i+1}`);
student.attend();
}
}
start();
student.js student.js
'use strict';
const EventEmitter = require('events');
class Student {
constructor(name) {
this.name = name;
}
attend() {
// simulate a student randomly asking a question within 10 minutes
let minutes = (Math.floor(Math.random() * 10) + 1) * 60000;
setTimeout(function() {
EventEmitter.emit('handRaised', 'What is the 2 + 3?');
}, minutes);
}
}
module.exports = Student;
When I run this, I get an error that says EventEmitter.emit is not a function
. 运行此命令时,出现错误
EventEmitter.emit is not a function
。 I've tried several variations without any luck. 我尝试了几种变体,没有任何运气。 What am I doing wrong?
我究竟做错了什么?
You can't use emit
on EventEmitter
directly. 您不能在
EventEmitter
直接使用emit
。 You need to have an instance of it. 您需要有一个实例。 For eg.:
例如:
const eventEmitter = new EventEmitter();
// Now, you're okay to go with emit
eventEmitter.emit('handRaised', 'What is the 2 + 3?');
To use the same instance, define it in a file and require wherever you'll need it. 要使用相同的实例,请在文件中定义它,并在需要的地方进行需求。 Then, you're safe to use
on
and emit
on it. 然后,您就可以安全地使用
on
并emit
它了。
The answer given by @Bhojendra Rauniyar is correct, imho, but is missing a working example which is given below. @Bhojendra Rauniyar给出的答案是正确的,恕我直言,但缺少下面给出的工作示例。 Note, a subtle, but important change I did to define the callback function for
setTimeout()
in student.js
: I am using the arrow function () =>
which binds this
of the student instance to the callback function. 请注意,我没有定义为回调函数微妙,但重要的改变
setTimeout()
在student.js
:我使用的是箭头函数() =>
结合this
学生实例的回调函数。 This is required to call the instance variable from the callback function. 这是从回调函数调用实例变量所必需的。 Alternatively
function () { ... }.bind(this)
can be used. 另外,可以使用
function () { ... }.bind(this)
。
index.js index.js
const EventEmitter = require('events');
const Student = require('./student');
const eventEmitter = new EventEmitter();
async function start() {
eventEmitter.on('newListener', (event, listener) => {
console.log(`Added ${event} listener.`);
});
eventEmitter.on('handRaised', (question) => {
console.log(question);
});
for (let i=0; i<10; i++) {
let student = new Student(`Student #${i+1}`, eventEmitter);
student.attend();
}
}
start();
student.js student.js
'use strict';
class Student {
constructor(name, eventEmitter) {
this.name = name;
this.eventEmitter = eventEmitter;
}
attend() {
// simulate a student randomly asking a question within 10 minutes
let minutes = (Math.floor(Math.random() * 10) + 1) * 60000;
setTimeout(() => {
this.eventEmitter.emit('handRaised', 'What is the 2 + 3?');
}, minutes);
}
}
module.exports = Student;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.