![](/img/trans.png)
[英]How can I export a string from a file and require() the exported string on another file in NODE.js?
[英]Node.js: How can I export value to another route?
如何将特定变量从一条路线导出到另一条路线? 此处的目的是导出变量并访问不同路由器文件中的值。
到目前为止,我已经尝试过,但没有用:
module.exports = {
router: router,
myVariabel: myVariabel
}
//瑞典人
您必须导出和导入变量。 假设从该文件导出一个 function 添加。
function add(x, y) {
return x + y;
}
function subtract(x, y) {
return x - y;
}
// Adding the code below to allow importing
// the functions in other files
module.exports = { add }
现在需要从另一个要使用这个 function 的文件中导入它。
// Importing the func.js module
// The ./ says that the func module
// is in the same directory as
// the main.js file
const f = require('./func');
// Require returns an object with add()
// and stores it in the f variable
// which is used to invoke the required
const result = f.add(10, 5);
console.log('The result is:', result);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.