简体   繁体   English

如何将类从一个文件导出到Node中的另一个文件?

[英]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.

相关问题 如何将函数从一个文件导出到另一个 node.js? - how to export a function from one file to another node.js? 如何从文件中导出字符串,并在导出的NODE.js文件中将require()导出到另一个文件中? - How can I export a string from a file and require() the exported string on another file in NODE.js? 如何从一个文件导出变量,并在另一个文件上require()导出的变量? - How can I export a variable from one file and require() the exported variable on another file? 如何将此变量从类方法导出到另一个文件? - How do I export this variable from class method to another file? 如何从一个索引文件中导出 node.js 中的所有模块、函数等? - How do i export all modules, functions, etc in node.js from one index file? 如何将一个类从一个文件嵌入到另一个文件? - How to embed a class from one file to another? 如何从其他文件创建另一个类的实例 - How can I create an instance of another class from a different file JS - 将数组从一个文件导出到另一个文件 - JS - Export array from one file to another 如何重用另一个JavaScript文件中的代码? - How can I reuse code from one JavaScript file in another? 如何使用另一个Javascript文件中的函数? - How can I use a function from one Javascript file in another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM