看,两个脚本标签,上面的js没用,下面的js有用,交换位置也是,上面的没用。没有错误报告。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
<h1>hello</h1>
<button id="yellowWin">create new window</button>
</body>
<script src="../../src/main/rightClick.js"></script>
<script src="../../src/render/yellowWin.js"></script>
</html>
我听不懂,请帮助我,谢谢。
rightClick.js是右键单击菜单,yellowWin.js是单击按钮创建一个新窗口,它们都调用了@electron/remote模块。
rightClick.js:
const {Menu} = require('@electron/remote')
const currentWindow = require('@electron/remote').getCurrentWindow();
const template = [
{
label: 'Copy',
accelerator: 'ctrl+c',
click: () => {
console.log('复制');
}
},
{
label: 'Cut',
accelerator: 'ctrl+x',
click: () => {
console.log('剪切');
}
},
{
label: 'Paste',
accelerator: 'ctrl+v',
click: () => {
console.log('粘贴');
}
}
];
const menu = Menu.buildFromTemplate(template);
window.onload = () => {
window.addEventListener('contextmenu', (e) => {
e.preventDefault();
menu.popup({window: currentWindow});
console.log('右键');
}, false)
}
yellowWin.js:
const {BrowserWindow} = require('@electron/remote')
const yellowWin = document.getElementById('yellowWin');
window.onload = ()=> {
yellowWin.onclick = ()=> {
let newWin = new BrowserWindow({
width: 400,
height: 200
})
newWin.loadFile('static/index/yellow.html')
newWin.on('closed', () => {
newWin = null
})
console.log('yellowWin');
}
}
1条答案
按热度按时间1mrurvl11#
Electron带有不同的security功能,默认情况下会激活。出于安全原因,您应该将NodeJS和Electron相关的代码保留在主进程上,而不是像这里一样尝试将其导入渲染器进程。最后,使用
@electron/remote
模块是不必要的,因为你已经可以用Electron的原生API做同样的事情。现在,对于您的特定情况,您需要使用IPC和preload来在渲染器和主进程之间进行通信。如果你想创建一个新的窗口按钮点击例如:
index.html
renderer.js
preload.js
main.js
更详细的例子可以in the docs,我建议你仔细阅读,因为你需要的所有信息都在这里。如果你很难理解IPC,我建议你看看this article,它很好地解释了一切是如何工作的。