Javascript 未捕获引用错误:未在filenae.html:(electron)中定义require
我正在使用electron 6.12.1编写一个基本的web应用程序 我在html文件脚本中面临的问题 未捕获引用错误:未定义require 在addWindow.html:21 (有两个窗口1是mainWindow.html,在其中可以选择打开第二个窗口addWindow.html) 下面是main.js的代码Javascript 未捕获引用错误:未在filenae.html:(electron)中定义require,javascript,html,electron,require,ipcrenderer,Javascript,Html,Electron,Require,Ipcrenderer,我正在使用electron 6.12.1编写一个基本的web应用程序 我在html文件脚本中面临的问题 未捕获引用错误:未定义require 在addWindow.html:21 (有两个窗口1是mainWindow.html,在其中可以选择打开第二个窗口addWindow.html) 下面是main.js的代码 const electron = require('electron'); const url = require('url'); const path = require('path
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行出现错误)
添加项目
在此处输入项目
添加项
常数电子=要求(“电子”);
常数{IPC}=电子;
const form=document.querySelector('form');
格式。添加的列表器(“提交”,提交格式);
函数提交形式(e){
e、 预防默认值();
控制台日志(123);
}
救命啊 使用导入不需要导入不工作
<!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>