繁体   English   中英

如何导入导出模块将值返回到其他js文件

[英]How to import export modules return values to other js file

试图掌握导出导入/导出过程,并将功能及其返回值转入另一个文件。 我遇到了这个很好的例子! 但是在将其转移到另一个文件时遇到了麻烦。

例如,在运行节点testing.js时出现此错误。 我以为您可以传递您的参数。

错误输出

console.log(testClass.authy)(app);
                             ^

ReferenceError: app is not defined

helper.js

module.exports.auth = function (app) {
    console.log("return authy")
    var app = "1";
    return app;
};

testing.js

const testClass = require('../commands/helper.js');
console.log(testClass.auth)(app);

首先,将函数的结果记录到控制台时,应该使用console.log(function()) ,而不要使用console.log(function)()

其次,您要传入' app '作为参数,这是您赋予函数的值,然后立即对其进行重新定义。 您的函数不需要任何参数,应该只是function() { ... } ,然后称为testClass.auth() 现在,您正在尝试将尚未定义的变量' app '传递到函数中。

因此,最后,您的代码应为:

helper.js

module.exports.auth = function () {
  console.log("return authy")
  var app = "1";
  return app;
};

testing.js

const testClass = require('../commands/helper.js');
console.log(testClass.auth());

app ”的值返回到console.log函数,然后显示它。 希望这可以帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM