如何在 Jhipster Angular 生成的应用程序上使用电子?

How to use electron on Jhipster Angular generated application?

我刚刚发现了 electron,我发现它很有趣,所以我想在我的 jhipster angular 项目(最新的 jhipster 版本)中实现它

我尝试按照本教程进行调整,但我相信由于 Jhipster 使用 Webpack,我的构建并不好

这是我所做的

我在src/main/webapp文件夹中声明了一个main.js文件

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

let win;

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

  // load the dist folder from Angular
  win.loadURL(
    url.format({
      pathname: path.join(__dirname, `/dist/index.html`),
      protocol: "file:",
      slashes: true
    })
  );

  // The following is optional and will open the DevTools:
  // win.webContents.openDevTools()

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

app.on("ready", createWindow);

// on macOS, closing the window doesn't quit the app
app.on("window-all-closed", () => {
  if (process.platform !== "darwin") {
    app.quit();
  }
});

// initialize the app's main window
app.on("activate", () => {
  if (win === null) {
    createWindow();
  }
});

然后我尝试如下更新我的配置文件

package.json

{
  "name": "boxing",
  "version": "0.0.1-SNAPSHOT",
  "main": "main.js", <-- added this
  "description": "Description for boxing",
  "private": true,
  "license": "UNLICENSED",
  "cacheDirectories": [
    "node_modules"
  ],
  "dependencies": {
    "@angular-devkit/build-angular": "^0.803.14", <-- installed using npm
    ...
  "scripts": {
    "electron": "ng build --base-href ./ && electron .",

angular.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "boxing": {
      "root": "",
      "sourceRoot": "src/main/webapp",
      "projectType": "application",
      "schematics": {
        "@schematics/angular:component": {
          "skipTests": true,
          "style": "scss"
        },
        "@schematics/angular:directive": {
          "skipTests": true
        },
        "@schematics/angular:guard": {
          "skipTests": true
        },
        "@schematics/angular:pipe": {
          "skipTests": true
        },
        "@schematics/angular:service": {
          "skipTests": true
        }
      },
      "prefix": "jhi",
      "architect": {
        <-- added this lines
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist"
          }
        }
       <-- end of add
      }
    }
  },
  "defaultProject": "boxing",
  "cli": {
    "packageManager": "npm"
  }
}

我终于将 index.html href 更新为 ./

但是当我在终端中 运行 命令时,我收到了这个错误

npm run electron

boxing@0.0.1-SNAPSHOT electron /home/housseyn/Desktop/projects/salle-de-sport ng build --base-href ./ && electron .

架构验证失败,出现以下错误: 数据路径“”应该需要 属性 'main'。 错误!代码生命周期 错误!错误号 1 错误!拳击@0.0.1-快照电子:ng build --base-href ./ && electron . 错误!退出状态 1 错误! 错误!在 boxing@0.0.1-SNAPSHOT 电子脚本处失败。 错误!这可能不是 npm 的问题。可能有额外的日志输出>above.

npm 错误!此 运行 的完整日志可在以下位置找到: 错误! /home/housseyn/.npm/_logs/2019-10-25T16_00_19_675Z-debug.log

您必须使用此命令 npm run electron-build 构建您的项目,然后将其添加到您的 package.json

脚本中

检查此 doc 直到结束

希望能帮到你,

使用这个配置:

{
          "name": "angular-electron-demo",
          "version": "0.0.0",
          "main": "main.js",
          "scripts": {
            "ng": "ng",
            "start": "ng serve",
            "build": "ng build",
            "test": "ng test",
            "lint": "ng lint",
            "e2e": "ng e2e",
            "start:electron": "ng build --base-href ./ && electron ."
          }, 
          // [...]
        }

添加后,可以使用start:electron脚本执行ng build --base-href ./ && electron 。首先构建项目,然后 运行 当前文件夹中的电子。

返回您的终端并运行:

npm run start:electron