如何使用多个静态方法导出类

问题描述 投票:0回答:2

我试图在node.js中导出这个类:

export class className {
  static method1(param1) {
    // do something
  }

  static method2(param1, param2) {
    // do something
  }
}

但它在终端中遇到以下错误:

complete_path ..... \ node_modules @ babel \ runtime \ helpers \ esm \ classCallCheck.js:1 [2](函数(exports,require,module,__ filename,__ dirname){export default function _classCallCheck(instance,Constructor){[ 2] ^^^^^^ [2] [2] SyntaxError:新脚本(vm.js:83:7)[2]在createScript(vm.js:267:10)[2]处出现意外的令牌[2] Object.runInThisContext(vm.js:319:10)[2]在Module._compile(internal / modules / cjs / loader.js:685:28)[2]在Object.Module._extensions..js(内部/模块) /cjs/loader.js:733:10)[2]在Module.load(internal / modules / cjs / loader.js:620:32)[2] 在tryModuleLoad(internal / modules / cjs / loader.js:560:12)[2]在Function.Module._load(internal / modules / cjs / loader.js:552:3)[2] 在Module.require(internal / modules / cjs / loader.js:658:17)[2] at require(internal / modules / cjs / helpers.js:22:18)[2] [nodemon] app崩溃 - 等待开始前文件更改...

node.js class export
2个回答
2
投票

使用module.exports而不是export

module.exports = class className {

 static method1(param1) {
     // do something
 }


 static method2(param1, param2) {
     // do something
 }

}

2
投票

Node.js尚不支持export关键字。你必须使用exportsmodule.exports

在你的情况下,你应该使用module.exports

module.exports = class className {
  static method1(param1) {
    // do something
  }

  static method2(param1, param2) {
    // do something
  }
}

有关exportsmodule.exports之间差异的更多信息,我建议你这个post

© www.soinside.com 2019 - 2024. All rights reserved.