在Node.js中声明多个module.exports

我要实现的目标是创建一个包含多个功能的模块。

module.js:

module.exports = function(firstParam) { console.log("You did it"); },
module.exports = function(secondParam) { console.log("Yes you did it"); }, 
// This may contain more functions

main.js:

var foo = require('module.js')(firstParam);
var bar = require('module.js')(secondParam);

我的问题是,这firstParam是一个对象类型,而这secondParam是一个URL字符串,但是当我遇到该问题时,它总是抱怨该类型是错误的。

在这种情况下,如何声明多个module.exports?

糊状物

您可以执行以下操作:

module.exports = {
    method: function() {},
    otherMethod: function() {},
};

要不就:

exports.method = function() {};
exports.otherMethod = function() {};

然后在调用脚本中:

const myModule = require('./myModule.js');
const method = myModule.method;
const otherMethod = myModule.otherMethod;
// OR:
const {method, otherMethod} = require('./myModule.js');

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章