运行“First Electron App”不会显示版本?

Har*_*lia 8 node.js electron

我正在遵循Electron 快速入门指南,它可以正常工作,没有任何错误,但输出与文档中的描述不同,document.write输出中不会显示带有的版本。

这是我的输出:

Hello World!

We are using node , Chrome , and Electron .
Run Code Online (Sandbox Code Playgroud)

我的预期输出将包括相应的版本号。

我检查了应用程序的 GitHub 页面,仍然相同,尝试了各种 StackOverflow 答案,但没有一个对我有用。

索引.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
We are using node <script>document.write(process.versions.node) </script>,
Chrome <script>document.write(process.versions.chrome)</script>,
and Electron <script>document.write(process.versions.electron) </script>.
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

包.json

{
    "name": "electronapp",
    "version": "1.0.0",
    "description": "",
    "main": "main.js",
    "scripts": {
       "start": "electron ."
    },
    "author": "harsh",
    "license": "ISC",
    "devDependencies": {
        "electron": "^5.0.2"
    }
}
Run Code Online (Sandbox Code Playgroud)

主文件

{
    "name": "electronapp",
    "version": "1.0.0",
    "description": "",
    "main": "main.js",
    "scripts": {
       "start": "electron ."
    },
    "author": "harsh",
    "license": "ISC",
    "devDependencies": {
        "electron": "^5.0.2"
    }
}
Run Code Online (Sandbox Code Playgroud)

我全局安装了 Node,Chrome 是用 Electron 打包的,对吧?

snw*_*flk 11

如果您激活开发者工具,您应该会在控制台中看到如下错误消息:

Uncaught ReferenceError: process is not defined
    at index.html:11
Run Code Online (Sandbox Code Playgroud)

您需要激活nodeIntegration和停用contextIsolation的的BrowserWindow,使过程中BrowserWindow运行(“渲染过程”)被允许接入节点的process对象。

mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
        nodeIntegration: true,
        contextIsolation: false
    }
})
Run Code Online (Sandbox Code Playgroud)

(电子12日之前,只有nodeIntegration需要密钥,默认值contextIsolationfalse。)

  • 万分感谢!我刚刚开始使用电子,无法弄清楚这一点,我该如何激活开发人员工具? (2认同)
  • 如果你有默认的应用程序菜单,你可以在那里做。或者在代码中:`mainWindow.toggleDevTools()`。 (2认同)