Uncaught ReferenceError: require не определен в filenae. html: (электрон) - PullRequest
0 голосов
/ 31 января 2020

Я пишу базовое c веб-приложение, использующее электрон 6.12.1. Проблема, с которой я сталкиваюсь в сценарии html file

Uncaught ReferenceError: require не определена в addWindow. html: 21

(есть два windows 1 для mainWindow. html, в котором можно открыть 2-е окно, которое является addWindow. html)

вот код main. js

const electron = require('electron');
const url = require('url');
const path = require('path');

const {app,BrowserWindow, Menu} = electron;


let mainWindow;
let addWindow;

//Listen for the app to be ready

app.on('ready',function()
{
    //create new window
    mainWindow = new BrowserWindow({
        webPreferences: {
            nodeIntegration: true,
            nodeIntegrationInWorker: true
          }
    });
    //load html file into the window
    mainWindow.loadURL(url.format(
    {
        pathname: path.join(__dirname,'mainWindow.html'),
        protocol: 'file:',
        slashes: true
    }));//above code behaving like " file://dirname/mainWindow.html"

    //quit app when closed(closes all subpages when close main page)
    mainWindow.on('closed',function(){
        app.quit();
    });
    //build menu from template
    const mainMenu = Menu.buildFromTemplate(mainMenuTemplate);

    //InsertMenu
    Menu.setApplicationMenu(mainMenu);
});

//handle create add window

function createAddWindow(){

    //create new window
    addWindow = new BrowserWindow({
        nodeIntegration: true,
        nodeIntegrationInWorker: true,
        width: 300,
        height: 200,
        title: 'Add Items'
    });
    //load html file into the window
    addWindow.loadURL(url.format(
    {
        pathname: path.join(__dirname,'addWindow.html'),
        protocol: 'file:',
        slashes: true,

    }));//above code behaving like " file://dirname/mainWindow.html"


    //garbage collection handler
    addWindow.on('close',function(){
        addWindow = null;
    });
}




//create menu template

const mainMenuTemplate = [

    {
        label:'File',
        submenu:[
            {
                label: 'Add Item',
                accelerator: process.platform == 'darwin' ? 'command+w' : 'CTRL+w',
                click(){
                    createAddWindow();
                }
            },
            {
                label: 'clear Item',
                accelerator: process.platform == 'darwin' ? 'command+a' : 'CTRL+a',
            },
            {
                label: 'Quit',
                accelerator: process.platform == 'darwin' ? 'command+Q' : 'CTRL+Q',
                click(){
                    app.quit();
                }
            },
        ]
    },
    {
        label: 'View'
    }
];

//IF Mac , add empty object to menu
if(process.platform == 'darwin')
{
    mainMenuTemplate.unshift({});//unshift will add element at the begining of the array
}

//add developer tools item if not in production
if(process.env.NODE_ENV !== 'production'){
    mainMenuTemplate.push({
        label: 'Developer tools',
        submenu:[
            {
                label: 'Toggle DevTools',
                accelerator: process.platform == 'darwin' ? 'command+i' : 'CTRL+i',
                click(item,focusedWindow){
                    focusedWindow.toggleDevTools();

                }
            },
            {
                role: 'reload'
            }
        ]

    });
}

пока это mainWindow. html

<!DOCTYPE html>
<html lang="en">
<head>

    <title>A basic app</title>
</head>
<body>
    <h1> hello world </h1>
</body>
</html>

теперь последнее, это addWindow. html (ошибка в строке № 21)

<!DOCTYPE html>
<html lang="en">
<head>
   <title> Add Items </title>
</head>
<body>
    <form>
        <div>
            <label> Enter item here </label>
            <input type="text" id="Item" autofocus>
        </div>

        <button type="submit">Add Item</button>
    </form>



   <script>


        const electron = require("electron");
        const {ipcRenderer} = electron;


        const form = document.querySelector('form');
        form.addEventListener('submit',submitForm);

        function submitForm(e){
            e.preventDefault();
            console.log(123);
        }

     </script>
</body>
</html>

помогите !!!

...