【问题标题】:How pass argument from one nodejs file to another?如何将参数从一个 nodejs 文件传递​​到另一个文件?
【发布时间】:2019-09-08 16:59:15
【问题描述】:

我有两个节点 js 文件“a 和 b”,我想将参数从文件 a.js 传递给文件 b.js,并将 b.js 的结果返回给 a.js

【问题讨论】:

    标签: node.js import


    【解决方案1】:

    您应该在文件 a.js 中创建一个函数并将其导出,然后从文件 b.js 中调用此函数并在参数中传递值。

    在 a.js 文件中

    function myFunction(value){
    //in value you will get your passes value
    }
    exports.myFunction = myFunction
    

    在 b.js 文件中

    const fileA = require('./a')
    fileA.myFunction('you value') 
    

    【讨论】:

      【解决方案2】:

      您可以通过将一个文件包含到另一个文件来做到这一点,例如在文件“a.js”中,您可以创建一个将参数作为输入并处理它的方法,然后返回结果,然后文件可以使用该结果“b.js”。这两种方式的通信可以通过函数调用来处理,请查看以下代码。

      文件 a.js

      function funcA(input)
      {
      console.log(">> Input Taking");
      console.log(input);
      var result = 1; 
      return result;
      
      }
      
      exports.funcA = funcA;

      文件 b.js

      const myFileA = require('./a');
      var response = myFileA.funcA("John Doe");
      console.log(response);

      【讨论】:

        猜你喜欢
        • 2021-04-24
        • 2020-07-10
        • 1970-01-01
        • 2015-09-07
        • 2014-07-02
        • 1970-01-01
        • 2017-01-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多