gpt4 book ai didi

javascript - 加载 Electron 标签模块时出错,无法在 Electron 中创建标签

转载 作者:IT老高 更新时间:2023-10-28 23:00:15 24 4
gpt4 key购买 nike

我已经安装了 Electron 模块包,用于在 Electron 中实现选项卡,如下所示

package.json

{
"name": "Backoffice",
"version": "1.0.0",
"description": "BackOffice application",
"main": "main.js",
"scripts": {
"start": "electron ."
},
"author": "Karthik",
"license": "ISC",
"devDependencies": {
"electron": "^2.0.8",
"electron-tabs": "^0.9.4"
}
}

main.js

const electron = require("electron");
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
const Menu = electron.Menu;
const path = require("path");
const url = require("url");
const TabGroup = require("electron-tabs");

let win;
const tabGroup = new TabGroup();

function createWindow() {
win = new BrowserWindow();
win.loadURL(url.format({
pathname:path.join(__dirname,'index.html'),
protocol:'file',
slashes:true
}));

win.on('closed',()=>{
win = null;
})
}

app.on('ready', function(){
createWindow();
const template = [
{
label : 'Backoffice',
submenu: [
{
label : 'Account Management',
click : function () {
let tab = tabGroup.addTab({
title: "Electron",
src: "http://electron.atom.io",
visible: true
});
}
},
{
label : 'HR Management',
click : function () {
console.log("CLICK HM menu");
}
},
]

}
]
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
});

index.html

<!DOCTYPE html>
<html lang="en">
<head>
<title>BackOffice</title>
<link rel="stylesheet" href="styles.css">
<link rel="stylesheet" href="node_modules/electron-tabs/electron-tabs.css">
</head>
<body>
<h1>BackOffice</h1>
<div class="etabs-tabgroup">
<div class="etabs-tabs"></div>
<div class="etabs-buttons"></div>
</div>
<div class="etabs-views"></div>
</body>
</html>

运行 npm start 时出现以下错误

App threw an error during loadReferenceError: document is not defined at Object.<anonymous> (C:\workspace\nodejs_workspace\electron\menu-demo\node_modules\electron-tabs\index.js:3:1)
at Object.<anonymous> (C:\workspace\nodejs_workspace\electron\menu-demo\node_modules\electron-tabs\index.js:421:3)
at Module._compile (module.js:642:30)
at Object.Module._extensions..js (module.js:653:10)
at Module.load (module.js:561:32)
at tryModuleLoad (module.js:504:12)
at Function.Module._load (module.js:496:3)
at Module.require (module.js:586:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (C:\DEV_2018\nodejs_workspace\electron\menu-demo\main.js:11:18)
  • 为什么我无法加载 Electron 模块包。

  • 是什么导致了这个错误?如何在点击时创建一个新标签 Electron 应用程序菜单?

最佳答案

正如@coolreader18 详细解释的那样,您必须在渲染器进程中使用electron-tabs

这意味着您必须在单击菜单项时通知 main.js 中的 html。 MenuItem的单击为您提供调用者 BrowserWindow,以便您可以向其发送消息。

main.js

 ...
{
label: 'Account Management',
click: function (menuItem, browserWindow, event) {
browserWindow.webContents.send('add-tab', {
title: 'Electron',
src: 'http://electron.atom.io',
visible: true
})
}
},
...

index.html

<body>
...
<script>
const { ipcRenderer } = require('electron')
const TabGroup = require('electron-tabs')
const tabGroup = new TabGroup()

ipcRenderer.on('add-tab', (event, arg) => {
tabGroup.addTab(arg)
})
</script>
</body>

关于javascript - 加载 Electron 标签模块时出错,无法在 Electron 中创建标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52252290/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com