Electron 常见问题 (FAQ)

    在大多数情况下,这些错误都是由网络问题导致,而不是因为 electron npm 包的问题。 如 ELIFECYCLEEAI_AGAINECONNRESETETIMEDOUT 等错误都是此类网络问题的标志。 最佳的解决方法是尝试切换网络,或是稍后再尝试安装。

    如果通过 npm 安装失败,您可以尝试直接从 直接下载 Electron。

    Electron 会在什么时候升级到最新版本的 Chrome?

    通常来说,在稳定版的 Chrome 发布后一到两周内,我们会更新 Electron 内的 Chrome 版本。 这个只是个估计且不能保证,取决于与升级所涉及的工作量。

    我们只使用稳定的 Chrome 频道。 如果一个重要的修复在 beta 或 dev 通道中,我们将返回端口。

    更多信息,请看安全介绍

    我们通常会在最新版的 Node.js 发布后一个月左右将 Electron 更新到这个版本的 Node.js。 我们通过这种方式来避免新版本的 Node.js 带来的 bug(这种 bug 太常见了)。

    Node.js 的新特性通常是由新版本的 V8 带来的。由于 Electron 使用的是 Chrome 浏览器中附带的 V8 引擎,所以 Electron 内往往已经有了部分新版本 Node.js 才有的崭新特性。

    如何在两个网页间共享数据?

    Alternatively, you can use the IPC primitives that are provided by Electron. To share data between the main and renderer processes, you can use the and ipcRenderer modules. To communicate directly between web pages, you can send a from one to the other, possibly via the main process using . Subsequent communication over message ports is direct and does not detour through the main process.

    This happens when the variable which is used to store the tray gets garbage collected.

    你可以参考以下两篇文章来了解为什么会遇到这个问题:

    如果你只是要一个快速的修复方案,你可以用下面的方式改变变量的作用域,防止这个变量被垃圾回收。

    改为

    1. const { app, Tray } = require('electron')
    2. let tray = null
    3. app.whenReady().then(() => {
    4. tray = new Tray('/path/to/icon.png')
    5. tray.setTitle('hello world')
    6. })

    我在 Electron 中无法使用 jQuery、RequireJS、Meteor、AngularJS。

    因为 Electron 在运行环境中引入了 Node.js,所以在 DOM 中有一些额外的变量,比如 moduleexportsrequire。 这导致 了许多库不能正常运行,因为它们也需要将同名的变量加入运行环境中。

    我们可以通过禁用 Node.js 来解决这个问题,在Electron里用如下的方式:

    1. <head>
    2. <script>
    3. window.nodeRequire = require;
    4. delete window.exports;
    5. delete window.module;
    6. </script>
    7. <script type="text/javascript" src="jquery.js"></script>
    8. </head>

    在使用 Electron 的提供的模块时,你可能会遇到和以下类似的错误:

    It is very likely you are using the module in the wrong process. 比如,electron.app 只能在主进程中使用, 然而 electron.webFrame 只能在渲染进程中使用。

    文字看起来很模糊,这是什么原因造成的?怎么解决这个问题呢?

    If sub-pixel anti-aliasing is deactivated, then fonts on LCD screens can look blurry. 示例:

    子像素反锯齿需要一个包含字体光图的图层的非透明背景。 (详情请参阅)

    为了实现这一目标,在 BrowserWindow的构造器中设置背景:

    1. const { BrowserWindow } = require('electron')
    2. const win = new BrowserWindow({
    3. backgroundColor: '#fff'
    4. })

    The effect is visible only on (some?) LCD screens. Even if you don’t see a difference, some of your users may. It is best to always set the background this way, unless you have reasons not to do so.