简体   繁体   English

Node.js,从不同文件访问 typescript static 变量

[英]Node.js, access typescript static variable from different files

I have a situation like this in a node.js project:我在 node.js 项目中有这样的情况:

test.ts测试.ts

export class Test {
  private static test: string = 'hallo';

  public static initialize() {
    Test.test = 'changed';
  }

  public static showTest() {
    console.log(Test.test);
  }
}

At the begin I initalize Test..一开始我初始化测试..

app.ts应用程序.ts

import { Test } from './test.ts'
...
Test.initialize();
Test.showTest();  // shows --> 'changed'

then I call a function of a class stored in another file otherFile.ts然后我调用存储在另一个文件 otherFile.ts 中的 class 的 function

import { Test } from './test.ts'
...
someFunction() {
  Test.showTest();  // shows --> 'hallo'
}

Is there a way to make the showTest() function show 'changed' also on the second call without reinitializing Test?有没有办法让 showTest() function 在第二次通话中也显示“已更改”而不重新初始化测试?

Thank you!谢谢! Davide戴维德

in this situation i prefer use localStorage because every time we import class, that reintilized在这种情况下,我更喜欢使用 localStorage,因为每次我们导入 class 时,都会重新启动

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

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