【发布时间】:2023-03-30 03:15:01
【问题描述】:
我到处搜索,但找不到如何创建我的电子应用程序的多个实例。我正在尝试创建一个 便签 应用程序,其中有一个加号窗口,假设创建一个新的便签,该便签又将具有相同的加号按钮。
我已经尝试过了,但是在第一次单击便笺上的加号按钮时会出现一个巨大的问题,会创建一个新实例,但在第二次单击加号按钮时会创建 2 个新实例,第三次单击加号按钮 4 时会出现新实例被创建
main.js 文件内
let mainWindow
function createWindow () {
// Create the browser window.
mainWindow = new BrowserWindow({width: 190, height: 190,frame:false,transparent:true})
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
}))
mainWindow.on('closed', function () {
mainWindow = null
})
//when plus button is clicked it is firing a message 'create-new-instance' through ipcRenderer
ipcMain.on('create-new-instance',()=>{
console.log('create new window');
createWindow();
})
}
//this is called when for the first time the app is launched
app.on('ready', createWindow)
加号按钮所在的renderer.js文件内
document.getElementById('plusButton').addEventListener("click", ()=>{
var {ipcRenderer} = require('electron')
ipcRenderer.send('create-new-instance')
})
问题是当我第一次单击加号按钮“create-new-instance”时,因为只有一个笔记实例(在您第一次启动应用程序时创建),所以创建了一个新笔记,使其成为 2 个笔记实例两者都能够收听“create-new-instance”事件,当我第二次单击加号按钮时,两个音符都会收听此事件并创建一个音符,每个音符总共有 4 个音符。
请帮助我或建议我任何其他替代方法:)
【问题讨论】:
标签: javascript github cross-browser cross-platform electron