我在一个模块中定义了一个类:
"use strict";
var AspectTypeModule = function() {};
module.exports = AspectTypeModule;
var AspectType = class AspectType {
// ...
};
module.export.AspectType = AspectType;
但我收到以下错误消息:
TypeError: Cannot set property 'AspectType' of undefined
at Object.<anonymous> (...\AspectType.js:30:26)
at Module._compile (module.js:434:26)
....
我应该如何导出这个类并在另一个模块中使用它?我看到了其他 SO 问题,但是当我尝试实现他们的解决方案时,我收到了其他错误消息。
最佳答案
// person.js
'use strict';
module.exports = class Person {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
display() {
console.log(this.firstName + " " + this.lastName);
}
}
// index.js
'use strict';
var Person = require('./person.js');
var someone = new Person("First name", "Last name");
someone.display();
关于javascript - 如何在 Node 4 中正确导出 ES6 类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32657516/