【问题标题】:Electron JS open window when click HTML Button单击 HTML 按钮时,Electron JS 打开窗口
【发布时间】:2021-07-01 07:29:14
【问题描述】:

我用谷歌搜索过,这似乎是一个经常性的任务,但我没有找到好的解决方案,这是我的代码。

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />

    <title>Hello World!</title>
  </head>
  <body>
    <h1 style="color: red">Todos</h1>
    <button type="button">Todos panel</button>
    <script>
      const btn = document.querySelector('button');
      button.addEventListener('click', () => {
        newtaskwindow();
      });
      function newtaskwindow() {
        const remote = require('electron').remote;
        const BrowserWindow = remote.BrowserWindow;
        const win = new BrowserWindow({
          height: 600,
          width: 800
        });

        win.loadFile('todos.html');
      }
    </script>
  </body>
</html>

这不起作用..我想点击按钮打开一个新的子窗口。

const { app, BrowserWindow, Menu } = require('electron');

function createWindow() {
  const win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true
    }
  });

  win.loadFile('index.html');
}

const todoWin = () => {
  const win = new BrowserWindow({
    width: 500,
    height: 400,
    modal: true,
    parent: ''
  });

  win.loadFile('todos.html');
};

// Menu
const isMac = process.platform === 'darwin';

const template = [
  ...(isMac
    ? [
        {
          label: 'Demo',
          submenu: [{ role: 'about' }]
        }
      ]
    : []),

  {
    label: 'File',
    submenu: [isMac ? { role: 'close' } : { role: 'quit' }]
  }
];

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

app.whenReady().then(() => {
  createWindow();
});

app.on('window-all-closed', function () {
  if (process.platform !== 'darwin') app.quit();
});

【问题讨论】:

    标签: electron


    【解决方案1】:

    我看到了两件事......

    在ma​​in.js中添加enableRemoteModule和contextIsolation

    function createWindow() {
      const win = new BrowserWindow({
        width: 800,
        height: 600,
        webPreferences: {
          nodeIntegration: true,
          enableRemoteModule: true,
          contextIsolation:false
    
        }
      });
    
      win.loadFile('index.html');
    }
    

    在 index.html 中,您使用 button.addEventListener 而不是 btn.addEventListener。改变它。

        <script>
          const btn = document.querySelector('button');
          btn.addEventListener('click', () => {
            newtaskwindow();
          });
          function newtaskwindow() {
            const remote = require('electron').remote;
            const BrowserWindow = remote.BrowserWindow;
            const win = new BrowserWindow({
              height: 600,
              width: 800
            });
    
            win.loadFile('todos.html');
          }
        </script>
    
    

    这种方式应该可行。


    警告
    正如 Electron 的文档中所述,remote 模块已被弃用。鼓励您使用IPC 以便在renderer 的请求下在main 上执行代码。

    【讨论】:

      【解决方案2】:
         document.querySelector('form').addEventListener('submit', (e) => {
          e.preventDefault();
          const { ipcRenderer } = require('electron');
          const value = document.querySelector('input').value;
          ipcRenderer.send('todo:save', value);
          window.close();
        });
      

      我是通过 ipcRenderer 完成的

      【讨论】:

        猜你喜欢
        • 2012-08-31
        • 2018-12-18
        • 1970-01-01
        • 1970-01-01
        • 2015-09-09
        • 1970-01-01
        • 2021-03-28
        • 2019-04-12
        • 2011-02-09
        相关资源
        最近更新 更多