如何在 Electron 14 的渲染器中获取当前浏览器 window

How to get the current browser window in the renderer in Electron 14

到目前为止,使用以下代码从 renderer 获取当前 window:

import {remote, BrowserWindow} from 'electron';

export function getCurrentWindow(): BrowserWindow {
    return remote.getCurrentWindow();
}

既然 electron 14 已经移除了 remote 模块,那么推荐的新方法是什么?

如果你想继续使用 remote 模块,你现在必须从 here. remote was stripped out (as per this discussion) 安装它并且现在在一个单独的用户空间仓库中维护,因为 Electron 团队没有不想鼓励这种模式,npm 页面说:

⚠️ Warning! This module has many subtle pitfalls. There is almost always a better way to accomplish your task than using this module. For example, ipcRenderer.invoke can serve many common use cases.

npm 页面进一步详细说明了如何安装和使用 remote 模块。

也就是说,推荐的策略是在主进程中执行所有 window 操作,并通过调用 ipcRenderer.invokeipcRenderer.send 从渲染器触发它。