electron 电子,我怎么才能使另一个窗口加载不同的文件

7d7tgy0s  于 2022-12-16  发布在  Electron
关注(0)|答案(1)|浏览(196)

我使用的是最基本的main.js文件,只做了一点修改,除了调用主窗口命令外,没有其他东西。我想让它看起来像是当点击一个按钮时,它会调用另一个窗口,加载一个不同的html文件。
main.js:

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

// Handle creating/removing shortcuts on Windows when installing/uninstalling.
// eslint-disable-next-line global-require
if (require('electron-squirrel-startup')) {
  app.quit();
}

const createWindow = () => {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 1200, //1196 + 4
    height: 714, //684 + 30
    resizable: false,
    autoHideMenuBar: true,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js'),
    },
    icon: path.join(__dirname, 'res/applogo.png')
  });
  // and load the index.html of the app.
  mainWindow.loadFile(path.join(__dirname, 'index.html'));
  
  // Open the DevTools.
  mainWindow.webContents.openDevTools();

};

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow);

// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});

app.on('activate', () => {
  // On OS X it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (BrowserWindow.getAllWindows().length === 0) {
    createWindow();
  }
});

index.html:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>my app</title>
    <link rel="stylesheet" href="index.css" />

    <script>
      document.querySelector('button').addEventListener('click', () => {
        window.open('devTool.html', "_blank"); // local file
      })
    </script>

  </head>
  <body>
    <script src="index.js"></script>

    <div class="application-container">

      <div class="menu-container">

        <button class="menu-button" data-title="Save Chart"></button>
        <button class="menu-button" data-title="Chart Information"></button>
        <button class="menu-button" data-title="Keybind Settings"></button>
        <button class="menu-button" data-title="Customization"></button>
        <button class="menu-button" data-title="Developer Tool"></button>

      </div>

      <div class="main-container">

        <div class="main-setting-container"></div>

        <div class="main-editor-container"></div>

        <div class="main-object-container"></div>

        

      </div>

      <div class="miscellaneous-container">

        <div class="miscellaneous-info-container"></div>

        <div class="miscellaneous-action-container"></div>

        <div class="miscellaneous-seperator-container">

          <div class="miscellaneous-seperator-left"></div>
          <div class="miscellaneous-seperator-right"></div>

        </div>

        <div class="miscellaneous-setting-container"></div>

      </div>

    </div>

    
  </body>
</html>

index.js是空的我已经尝试了一些解决方案(window.opennewWindowopenWindow())来制作这个,但是在点击按钮后都不起作用。相反,在我单击按钮之前,窗口打开了(它自动加载)。我想知道像我怎么能使它与按钮从上面的html代码(如果可能的话与按钮)或哪个错误,我可以与3个例子以上谢谢你阅读这到目前为止,请帮助我,如果你可以。

bakd9h0s

bakd9h0s1#

只需像这样添加window.open,然后在body的末尾添加<script>

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>my app</title>
    <link rel="stylesheet" href="index.css" />

  </head>
  <body>
    ........................
    <button>Open new window</button>
    <script>
       document.querySelector('button').addEventListener('click', () => {
            window.open('index.html', null, "width=1200,height=714,resizable=1"); // local file
       })
    </script>
  </body>
</html>

添加图标到每个窗口添加到main.js

app.on("browser-window-created", (event, window) => {
    window.setIcon(path.join(__dirname, 'res/applogo.png'))
});

相关问题