Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 在渲染器中导入与主进程中相同的对象_Node.js_Typescript_Electron - Fatal编程技术网

Node.js 在渲染器中导入与主进程中相同的对象

Node.js 在渲染器中导入与主进程中相同的对象,node.js,typescript,electron,Node.js,Typescript,Electron,我在单独的文件discovery.ts中有一个自定义类,其中我的代码如下所示 import { EventEmitter } from 'events'; class Discovery extends EventEmitter { let myProperty = 'value'; constructor { super(); } } let disc = new Discovery(); export { disc }; 现在我有一个main.ts文件,代表Electron的

我在单独的文件
discovery.ts
中有一个自定义类,其中我的代码如下所示

import { EventEmitter } from 'events';
class Discovery extends EventEmitter {
   let myProperty = 'value';

   constructor { super(); }
}
let disc = new Discovery();
export { disc };
现在我有一个
main.ts
文件,代表Electron的主要进程:

import { app, Tray, Menu, BrowserWindow, ipcMain, screen } from 'electron';
import { disc } from './discovery';

let window: Electron.BrowserWindow = null;
app.on('ready', () => {
  ////
  disc.myProperty = 'new value'; // Changing value here
  ////

  window = new BrowserWindow({width: 800, height: 600});
  window.loadURL('file://' + __dirname + '/../html/index.html';);
});
以及表示Electron渲染器进程的
index.ts
文件(该文件作为脚本从
index.html
导入):

现在的问题是,我希望
Discovery
类在导入
'./Discovery'
的所有文件中都是同一个对象,但是当我在
main.ts
中为
disc.myProperty
分配一个新值时,
console.log(disc.myProperty)
index.ts
中仍然打印一个旧值-“value”


据我所知,我认为模块只执行一次,所以当您在多个文件中导入一个对象时,您在每个文件中导入相同的对象,并且该对象在所有文件中的状态都相同。我错了吗?或者这个问题可能与Electron有关?

您可以跨进程共享一个实例。@SLaks您能告诉我或给我指出正确的方向来实现这一点吗?对不起;我的意思是你不能。因此,实现类似于这一点的唯一方法是使用
index.ts
中的
ipc
main.ts
进行通信,然后从
main.ts
将需求委托给
Discovery
import { disc } from './discovery';
console.log(disc.myProperty); // Should print 'new value' but prints 'value'