我正在尝试开始使用 Electron。我已经能够运行所有简单的示例。它们都按预期工作。当我尝试按照快速入门指南我遇到同样的问题,因为在提到这个问题:应用程序启动正常,但不显示节点Chrome和电子的版本。当我查看开发工具时,我看到了这个错误:
Uncaught ReferenceError: process is not defined
at index.html:14
Run Code Online (Sandbox Code Playgroud)
但是,我已经设置nodeIntegration为true.
为什么它仍然不起作用?
版本:
操作系统:
索引.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
<meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" />
</head>
<body style="background: white;">
<h1>Hello World!</h1>
<p>
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>.
</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
主文件
const { app, BrowserWindow } = require('electron')
function createWindow …Run Code Online (Sandbox Code Playgroud) 我有一个电子应用程序。我需要从本地计算机获取环境变量的值以在渲染器线程上使用。我想在应用程序启动时将此值传递给渲染器线程。我知道在main.js文件中,我可以像这样获取环境变量:
main.js
const createWindow = () => {
const win = new BrowserWindow({
width: 800,
height: 600
});
win.loadFile('index.html');
// Retrieve the environment variable value
const variableValue = process.env.MY_ENVIRONMENT_VARIABLE;
console.log(`Variable value: ${variableValue}`);
// Send the variable and it's value to the renderer.
win.webContents.send('initialize', { 'variableName':variableValue });
}
Run Code Online (Sandbox Code Playgroud)
上面成功地将环境变量的值打印到终端窗口。但是,我需要在浏览器端使用这个值。我如何在那里获取变量的值?
谢谢你!