【问题标题】:How to set up an Electron project based on AngularCLI, with debugging/live reload?如何设置基于 AngularCLI 的 Electron 项目,并进行调试/实时重新加载?
【发布时间】:2017-07-29 05:32:47
【问题描述】:

我正在尝试设置一个 AngularCLI 项目,该项目可以在浏览器中和通过电子运行。

我找到了一些很好的教程,比如这篇文章:

http://www.blog.bdauria.com/?p=806

但是我找不到或找出允许在电子项目中实时重新加载和调试的设置。

【问题讨论】:

    标签: angular electron angular-cli


    【解决方案1】:

    假设您使用的是@angular/cli

    • src文件夹旁边创建一个文件夹electron
    • 在此文件夹中,创建一个文件electron.js

    在其中粘贴以下内容:

    const {app, BrowserWindow} = require('electron');
    
    // keep a global reference of the window object, if you don't, the window will
    // be closed automatically when the JavaScript object is garbage collected
    let win;
    
    function createWindow() {
      // create the browser window
      win = new BrowserWindow();
      win.maximize();
    
      // and load the index.html of the app
      win.loadURL(`file://${__dirname}/index.html`);
    
      // ----------------------------------------------------------------
      //           THIS IS WHAT YOU'RE LOOKING FOR DEBUGGING
      // open the DevTools
      win.webContents.openDevTools()
      // ----------------------------------------------------------------
    
      // emitted when the window is closed
      win.on('closed', () => {
        // fereference the window object, usually you would store windows
        // in an array if your app supports multi windows, this is the time
        // when you should delete the corresponding element
        win = null;
      });
    };
    
    // yhis 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
    app.on('window-all-closed', () => {
      // on macOS it is common for applications and their menu bar
      // to stay active until the user quits explicitly with Cmd + Q
      if (process.platform !== 'darwin') {
        app.quit();
      }
    });
    
    app.on('activate', () => {
      // on macOS 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 (win === null) {
        createWindow();
      }
    });
    
    • 在同一文件夹中,创建一个文件generate-package-json.json。这样我们就可以像这样为我们的电子应用程序自动生成包:

    文件:

    const fs = require('fs');
    
    const package = `{ "name": "${ require('../package.json').name}", "version": "${ require('../package.json').version}", "main": "electron.js" }`;
    
    fs.writeFile('./dist/package.json', package, err => {
      if (err) {
        return console.log(err);
      }
    });
    

    然后在你的 package.json 中:

    {
      ...
      scripts: {
        "build-electron": "npm run build -- -bh='./'; node ./electron/generate-package-json.js; cp ./electron/electron.js dist/",
        "electron": "npm run build-electron; electron dist/"
      }
      ...
    }
    

    最后,运行npm run electron

    【讨论】:

    • 感谢马克西姆的回复。我是否在 src 文件夹“内部”创建电子文件?
    • 不客气。不,在电子文件夹中:electron/electron.js
    • 我尝试了您的解决方案,它有效,但没有解决问题。我正在寻找可以实时重新加载的东西,我将编辑我的帖子以指定。
    猜你喜欢
    • 1970-01-01
    • 2018-07-09
    • 1970-01-01
    • 1970-01-01
    • 2017-02-11
    • 1970-01-01
    • 1970-01-01
    • 2022-07-04
    • 1970-01-01
    相关资源
    最近更新 更多