Javascript electron.remote在渲染过程中未定义
当用户单击按钮时,我试图使用dialog,bug得到一个错误Javascript electron.remote在渲染过程中未定义,javascript,electron,Javascript,Electron,当用户单击按钮时,我试图使用dialog,bug得到一个错误未捕获类型错误:无法读取未定义的属性“dialog”。控制台日志的结果是未定义 main.js文件 const { app, BrowserWindow } = require('electron'); const path = require('path'); if (require('electron-squirrel-startup')) { app.quit(); } const createWindow = () =&
未捕获类型错误:无法读取未定义的属性“dialog”。控制台日志的结果是未定义
main.js
文件
const { app, BrowserWindow } = require('electron');
const path = require('path');
if (require('electron-squirrel-startup')) {
app.quit();
}
const createWindow = () => {
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true
}
});
mainWindow.loadFile(path.join(__dirname, './src/index.html'));
mainWindow.webContents.openDevTools();
};
app.on('ready', createWindow);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>welcome</title>
<link rel="stylesheet" href="index.css">
</head>
<body>
<p id="create">Create New File</p>
<p id="open">Open File</p>
<script src="./index.js"></script>
</body>
</html>
const { remote } = require('electron');
console.log(remote); // undefined
const open = document.querySelector('#open');
const create = document.querySelector('#create');
open.addEventListener('click', function () {
remote.dialog.showErrorBox('error', '123');
});
index.html
文件
const { app, BrowserWindow } = require('electron');
const path = require('path');
if (require('electron-squirrel-startup')) {
app.quit();
}
const createWindow = () => {
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true
}
});
mainWindow.loadFile(path.join(__dirname, './src/index.html'));
mainWindow.webContents.openDevTools();
};
app.on('ready', createWindow);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>welcome</title>
<link rel="stylesheet" href="index.css">
</head>
<body>
<p id="create">Create New File</p>
<p id="open">Open File</p>
<script src="./index.js"></script>
</body>
</html>
const { remote } = require('electron');
console.log(remote); // undefined
const open = document.querySelector('#open');
const create = document.querySelector('#create');
open.addEventListener('click', function () {
remote.dialog.showErrorBox('error', '123');
});
添加启用远程模块
。我认为您使用的是最新版本的Electron,默认情况下,我们无法在渲染器上使用remote
模块。需要添加此标志才能启用此功能
参考:
添加启用远程模块
。我认为您使用的是最新版本的Electron,默认情况下,我们无法在渲染器上使用remote
模块。需要添加此标志才能启用此功能
Ref:建议不要使用预加载脚本。建议不要使用预加载脚本。