main.js
mainWindow = new BrowserWindow({ show: false, minWidth: 400, minHeight: 400, webPreferences: {nodeIntegration: true} });
mainWindow.maximize();
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, '/html/index.html'),
protocol: 'file:',
slashes: true
}));
mainWindow.on('closed', function(){
app.quit();
});
const mainMenu = Menu.buildFromTemplate(mainMenuTemplate);
Menu.setApplicationMenu(mainMenu);
mainWindow.once('ready-to-show', () => {
mainWindow.show();
const data = {id: 'checking_for_updates', display: 'none'}
console.log(data)
mainWindow.webContents.send('change_display', data)
})
index.html
<script>
const electron = require('electron');
const ipcRenderer = electron.ipcRenderer;
ipcRenderer.on('change_display', function (event, data) {
console.log(data);
document.getElementById(data.id).style.display=data.display;
})
</script>
<body>
<!-- Checking if you can update -->
<div class="container updating" id="checking_for_updates" style="display: block">
<h1 class="updating-text">Checking for updates.</h1>
</div>
它仅记录一次“数据”,因此ipcRenderer可能不会在change_display事件上运行ipcRenderer.on。我该如何解决这个问题?
答案 0 :(得分:0)
您的代码在这里:
mainWindow.once('ready-to-show', () => {
mainWindow.show();
const data = {id: 'checking_for_updates', display: 'none'}
console.log(data)
mainWindow.webContents.send('change_display', data)
})
仅发送一次change_display
。因此,您还会看到data
也被记录一次。