简体   繁体   English

电子:在mainWindow加载时使用mainWindow.webContents.send

[英]Electron: Using mainWindow.webContents.send when mainWindow loads

When a user changes the theme, I use mainWindow.webContents.send to change a class in the DOM. 当用户更改主题时,我使用mainWindow.webContents.send更改DOM中的类。 I also save it in the store, under the key theme . 我还将其保存在商店中, theme

mainWindow.webContents.send('theme:change', theme);
store.set('theme', theme);

Then in renderer.js : 然后在renderer.js

ipcRenderer.on('theme:change', (event, theme) => {
  document.querySelector('body').className = `${theme}`;
});

This successfully changes the theme and saves it in the store. 这样可以成功更改主题并将其保存在商店中。 However, now I want that theme to load up when starting the application rather than going back to the default. 但是,现在我希望在启动应用程序时加载该主题,而不是返回到默认主题。 To do this, in app.on('ready') I am doing this: 为此,在app.on('ready')我正在这样做:

mainWindow.webContents.send('theme:change', store.get('theme'));

However, nothing is happening. 但是,没有任何反应。 It's like it isn't being sent. 好像没有被发送。 Where am I going wrong? 我要去哪里错了? Essentially what needs to be done is for the class in body to be changed when the application loads to the one in the store. 本质上,需要做的是在应用程序加载到商店中的body时更改body的类。

Figured it out. 弄清楚了。 I had to put: 我不得不说:

mainWindow.webContents.once('dom-ready', () => {
    mainWindow.webContents.send('theme:change', store.get('theme'));
})

I was trying mainWindow.on('dom-ready') which is why it wasn't working. 我正在尝试mainWindow.on('dom-ready') ,这就是为什么它不起作用的原因。

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

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