【问题标题】:Function calls after the imported URL导入 URL 后的函数调用
【发布时间】:2020-01-02 14:39:28
【问题描述】:

我编写了一个代码,以便在启动期间在本地存储中保存一些值。我在index.ts 中有这个函数,还有一个名为abc.ts 的另一个页面的导入url。在该页面中,我使用了在启动期间保存在本地存储中的值。我的问题是 URL 之前的函数调用,abc.ts 中的值显示为undefined

index.ts

var cache = require('store')
getEnvironmentVariables();
require('./abc.ts');
function getEnvironmentVariables() {
 cache.set('ENV', 'XXXXXXXXXXX');
}

abc.ts

var cache = require('store')
console.log(cache.get('ENV'));

我还有其他文件在启动后运行。我可以用 cache.get('ENV') 完美。

【问题讨论】:

    标签: javascript node.js typescript local-storage


    【解决方案1】:

    问题在于 Node.js 正在异步执行您的代码,为了让您的用例正常工作,您可以尝试这样的操作:

    abc.ts

    var cache = require('store')
    setTimeout(() => {
      console.log(cache.get('ENV'));
    }, 1000);
    

    【讨论】:

      猜你喜欢
      • 2012-10-21
      • 1970-01-01
      • 2018-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-11
      相关资源
      最近更新 更多