我们可以在一个js文件中有多个module.exports语句吗

can we have more than one module.exports statements in one js file

本文关键字:module 语句 exports 文件 一个 我们 js      更新时间:2023-09-26

我是javascript新手。我正在使用Titanium studio开发移动应用程序。我的问题是关于javascript,如下所示。

我在这个文件中有一个名为commonUi.js的js文件,我正在定义可以在不同窗口中使用的通用UI组件。

现在我在commonUi.js中定义两个对象函数,如下所示。

function component1(){
}
function component2(){
}

现在我的核心问题是,我能在我的commonUi.js文件中写以下两条语句吗

  1. module.exports=组件1
  2. module.exports=组件2

非常感谢您的帮助。

您的模块中只能有一个module.exports =,但您可以这样做来实现您想要的:

var CommonUi = function() {
    var component1 = function() {
    }
    var component2 = function() {
    }
    return {
        component1: component1
       ,component2: component2
    }
}();
module.exports = CommonUi;

然后你可以这样使用它:commonUi.component1();