Node.js 在渲染器之间传递变量

Node.js 在渲染器之间传递变量,node.js,electron,Node.js,Electron,我想在electron中的两个html文件之间传递变量,下面是我尝试过的,但什么也没发生。我做错什么了吗 main.js: const {app , BrowserWindow} = require('electron'); const ipc = require('electron').ipcMain; app.on('ready', function(){ mainWindow = new BrowserWindow({ webPreferences: {

我想在electron中的两个html文件之间传递变量,下面是我尝试过的,但什么也没发生。我做错什么了吗

main.js:

const {app , BrowserWindow} = require('electron');
const ipc = require('electron').ipcMain;
app.on('ready', function(){
    mainWindow = new BrowserWindow({
        webPreferences: {
            nodeIntegration: true
        }
    });
    mainWindow.on('closed',()=>{
        mainWindow = null;
        });
    mainWindow.webContents.loadFile(__dirname + '/index.html');
});
    ipc.on('set_token', function(event, arg){ 
   mainWindow.webContents.send('tokens', arg);
});
firstfile.js:

const ipc = require('electron').ipcRenderer;
ipc.send('set_token', "someValue");
secondfile.js:

const ipc = require('electron').ipcRenderer;
const tes = document.getElementById("send");
const ipc = require('electron').ipcRenderer;
tes.addEventListener('click', function(event){
    var hi = document.getElementById("hi");
    ipc.on('tokens', function(event, arg){
    hi.innerHTML = arg;
    });
  });
});

“index.html”中是否包含“secondfile.js”?另外,您是否尝试过在代码中设置断点/调试器以查看失败的地方?@Seblor感谢您的评论,是的,它工作正常,只是它没有从IPC部分给我任何答案。。。不,我想在两个独立于index.html的html文件之间传递变量,是不是我只能在index.html和其他文件之间共享变量?我不能在两个单独的html文件之间共享变量吗?@Seblor而且我没有收到任何错误,这是令人沮丧的部分,但是你的html文件在哪里?您想在两个html文件之间共享日期,但它们是否已加载到browserwindows中?我们错过了一些代码。