电子重新加载命令的特定JS文件

时间:2018-01-09 23:47:02

标签: javascript node.js electron reload

我最近一直在研究一种Electron应用程序,它需要在javascript文件中存储数据,该文件在用户登录和显示时被解密,并在用户注销时加密。但是,登录后,用户可以选择将数据添加到javascript文件中。但不幸的是,当此过程完成时,尽管在初始显示中使用与重新加载中完全相同的代码,但不会显示新数据。我确信这是由于需要重新加载的javascript文件(Electron未注册文件更改)。我尝试了electron-reload模块,但它似乎只允许实时重新加载。我需要一个模块或解决方案,允许我做这样的事情。

var reload = require('some-reload-module');
reload.reload('../path/to/file.js');
...

任何解决方案都会受到欢迎,因为到目前为止我没有运气。提前谢谢!

1 个答案:

答案 0 :(得分:0)

由于require.cacherequire caches its results的事实,这种情况正在发生。要解决这个问题,您可以 delete the entry in the cache

// Initially require the file; the result is cached.
require('../path/to/file.js');

// Delete the cached version of the module.
delete require.cache[require.resolve('../path/to/file.js')];

// Re-require the file; the file is re-executed and the new result is cached.
require('../path/to/file.js');