我想要实现的是创建一个包含多个功能的模块。
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');
关于node.js - 在 Node.js 中声明多个 module.exports,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16631064/