简体   繁体   English

在 node.js 中的文件之间共享变量

[英]Sharing variables between files in node.js

I want to make "variable sharing" in node.js but I cannot find a working way to do that.我想在 node.js 中进行“变量共享”,但我找不到可行的方法来做到这一点。

My aim is to be able to change variables from an another file.我的目标是能够从另一个文件中更改变量。 For example, I have a in my main file but after doing something in another file ( test.js ), a should change its value in the main file.例如,我的主文件中有a但在另一个文件 ( test.js ) 中执行某些操作后, a应该在主文件中更改其值。 Is it possible to accomplish and how?是否有可能完成以及如何完成?

I have tried this code but it does not seem to work.我已经尝试过这段代码,但它似乎不起作用。

main.js

let a = 10;
module.exports.test = a;
console.log(a);
require('./test.js');
console.log(a);

test.js

let b = require('./main.js').test;
b = 15;

Other answers given here suggest using globals, but you should avoid use of globals for a whole host of reasons which I won't go into here, but are well documented.此处给出的其他答案建议使用全局变量,但您应该避免使用全局变量,原因有很多,我不会 go 在这里讨论,但有据可查。

The modular and safe way to share data is to put it in an object (as properties of an object) and then export that object from one module and import it into any others that want access to it.共享数据的模块化和安全方法是将其放入 object(作为对象的属性)中,然后从一个模块中导出 object 并将其导入到任何其他想要访问它的模块中。 Since the exported object is truly shared, any update to any property on that object will be immediately available to all others who have a reference to the same object.由于导出的 object 是真正共享的,因此对该 object 的任何属性的任何更新都将立即可供所有其他引用相同 object 的人使用。

shared-data.js

module.exports = {a: 10};

test.js

let obj = require('./shared-data.js');
++obj.a;

main.js

let obj = require('./shared-data.js');
console.log("main1: ", obj.a);
++obj.a;
console.log("main2: ", obj.a);

require('./test.js');
console.log("main3: ", obj.a);

This will output:这将 output:

main1: 10
main2: 11
main3: 12

Because test.js is operating on the shared data that shared-data.js exports因为 test.js 是对 shared-data.js 导出的共享数据进行操作

You can just use global variables.您可以只使用全局变量。 See more here在这里查看更多

main.js

global.a = 10;
console.log(global.a); // 10
require('./test.js');
console.log(global.a); // 15

test.js

console.log(global.a) // 10
global.a = 15

The bad idea here is think when you call ./test , magically variable a will be updated.这里的坏主意是认为当你调用./test时,神奇的变量a将被更新。

Here you can use global variables ( example )在这里您可以使用global变量( 示例

Something like:就像是:

main.js main.js

global.a = 10
//call test
console.log(global.a)) // 15

test.js测试.js

global.a = 15

And, following you idea, calling another class and using these values, you can also do something like this:而且,按照您的想法,调用另一个 class 并使用这些值,您还可以执行以下操作:

main.js main.js

var a = 10
a = require('./test').a //Note that here you are overwriting your 'a' variable
console.log(a) //15

test.js测试.js

var b = 15
module.exports.a = b

So, into main.js you assign a value that is from module.exports.a , ie you assing a = 15 .因此,在main.js ,您分配a来自module.exports.a的值,即您分配a = 15

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

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