0

我正在使用电子 6.12.1 编写一个基本的 Web 应用程序我在 html 文件的脚本中遇到的问题

未捕获的 ReferenceError:在 addWindow.html:21 中未定义要求

(有两个窗口 1 是 mainWindow.html,其中打开第二个窗口的选项是 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>

帮助 !!!

4

0 回答 0