[英]How can I export a class from one file to another file in Node?
Imagine I have this class: 想象一下我有这堂课:
class Class {
constructor(arg1, arg2) { arg1 = arg2};
}
Should I do this? 我应该这样做吗?
class Class = exports.Class {
constructor(arg1, arg2) { arg1 = arg2};
}
Or there's another way? 还是有另一种方式?
With export
syntax, just put export
before the class: 使用
export
语法,只需将export
放在类之前:
export class Class {
(this results in a named export named Class
) (这将导致名为
Class
的命名导出)
Or, for a default export: 或者,对于默认导出:
export default class Class {
With module
syntax, assign to module.exports
, or to a property of module.exports
: 随着
module
语法,分配给module.exports
,或者对一个属性module.exports
:
module.exports = class Class {
or 要么
module.exports.Class = class Class {
You should do like this (for other ways, check @Snow answer): 您应该这样做(对于其他方式,请检查@Snow答案):
class Class {
constructor(arg1, arg2) { arg1 = arg2};
}
module.exports = Class;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.