Electron.js Ошибка: требование не определено - PullRequest
2 голосов
/ 10 марта 2019

Я пишу свое первое электронное приложение, и я пытался показать окно при нажатии кнопки. У меня есть сообщение об ошибке:

Uncaught ReferenceError: требование не определено at dialog.js: 2

Я использую версию "electronic-nightly": "^ 6.0.0-nightly.20190213"

вот код:

index.js:

const electron = require('electron');
const {app, BrowserWindow} = electron;
const path = require('path');
const url = require('url');

let win

function main(){

    win = new BrowserWindow({ width: 500, height: 400});

    win.loadURL(url.format( {
        pathname: path.join(__dirname, './index.html'),
        protocol: 'file',
        slashes: true
    } ));

    win.webContents.openDevTools()

}

exports.openDialog = () => {
    let dial = new BrowserWindow({ width: 400, height: 200});

    dial.loadURL(url.format( {
        pathname: path.join(__dirname, './dialog.html'),
        protocol: 'file',
        slashes: true
    } ));
}


app.on('ready', main);

index.html:

 <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Hello world</title>
</head>
<body>

    <h1>Hello Electron!!</h1>

    <button id="btn">Show dialog</button>

    <script src="./dialog.js"></script>

</body>
</html>

dialog.js:

const index = require('electron').remote.require('./index.js'); //Error line: Uncaught ReferenceError: require is not defined at dialog.js:2

const button = document.getElementById('btn');

button.addEventListener('click', () => {
    index.openDialog();
});

Это ошибка в ES6 +?

1 Ответ

0 голосов
/ 18 июля 2019

Возможно, вам придется включить Интеграцию узлов в новом окне (по умолчанию отключено в соответствии с документацией ):

index.js

function main() {

  win = new BrowserWindow({
    width: 500,
    height: 400,
    webPreferences: {
      nodeIntegration: true
    }
  });

  win.loadFile("index.html") // To load local HTML files easily

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...