用途:本资料是 Electron 跨平台桌面应用开发模式参考,适合构建 Electron 应用、处理主进程/渲染进程、IPC 通信及原生系统集成时使用。
主要能力与关键模式: 安全配置:创建 BrowserWindow 时使用 contextIsolation: true、nodeIntegration: false,可配合 preload 与 sandbox: true;禁止开启 nodeIntegration,禁止使用已废弃的 remote 模块。 IPC 通信:通过 preload 中 contextBridge.exposeInMainWorld 暴露具名函数;双向通信使用 ipcMain.handle() 配合 ipcRenderer.invoke(),单向使用 ipcRenderer.send() 与 ipcMain.on(),主进程向渲染进程推送用 win.webContents.send() 与 ipcRenderer.on()。 典型场景:文件读写走主进程 IPC handler(如 read-file、write-file),原生对话框用 dialog.showOpenDialog(),自动更新用 electron-updater 并显式监听 update-available、download-progress、update-downloaded 等事件,原生菜单用 Menu.buildFromTemplate()。
限制与规则:所有 IPC 必须经过 preload 中定义的具名通道,不得向渲染进程暴露完整 ipcRenderer;不应放宽上述安全设置;文件 I/O、网络等阻塞或耗时操作应放在主进程;原生 OS 集成应在主进程生命周期内设置。资料未提供价格、平台支持范围或具体版本要求等信息。