加载电子标签模块时出错,无法在电子中创建标签

Kar*_*hik 17 javascript node.js npm 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"
  }
}
Run Code Online (Sandbox Code Playgroud)

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);
});
Run Code Online (Sandbox Code Playgroud)

的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>
Run Code Online (Sandbox Code Playgroud)

当我运行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)
Run Code Online (Sandbox Code Playgroud)
  • 为什么我无法加载电子模块包.

  • 导致此错误的原因是什么?如何在电子中单击应用程序菜单创建新选项卡?

per*_*rgy 7

正如@coolreader18 详细解释的,你必须electron-tabsRenderer 进程中使用

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

主文件

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

索引.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>
Run Code Online (Sandbox Code Playgroud)


coo*_*r18 5

文档electron-tabs,它提到从渲染器进程调用它,但是你在主进程中进行它.主要过程是控制电子apis,例如打开你所在的窗口main.js.每个浏览器窗口都会创建一个新的渲染器进程,该进程可以与主进程通信或管理自己的文档和Web APIS.

您遇到的错误document is not defined是因为主进程无法访问DOM,因为您可以从同一主进程打开多个浏览器; 它不知道使用哪个.所以你需要做的是在渲染器过程中放置​​一个脚本.创建一个renderer.js,并将electron-tabs代码(const TabGroup = require("electron-tabs");)放在那里.然后,在你的index.html,放<script src="renderer.js"></script>,它应该工作.