Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/435.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript electron.remote在渲染过程中未定义_Javascript_Electron - Fatal编程技术网

Javascript electron.remote在渲染过程中未定义

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,bug得到一个错误
未捕获类型错误:无法读取未定义的属性“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:

建议不要使用预加载脚本。建议不要使用预加载脚本。