gpt4 book ai didi

Angular 4 + Electron - 如何运行应用程序并观察变化(实时重新加载)

转载 作者:太空狗 更新时间:2023-10-29 17:27:33 25 4
gpt4 key购买 nike

我正在使用 Angular 4 创建一个 Electron 应用程序。我如何设置它以便它监视任何更改并实时重新加载它。

包.json

{
"name": "angular-electron",
"version": "0.0.0",
"license": "MIT",
"main": "main.js",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e",
"electron": "electron .",
"electron-build": "ng build --prod && electron ."
},
"private": true,
"dependencies": {
"@angular/animations": "^4.2.4",
"@angular/common": "^4.2.4",
"@angular/compiler": "^4.2.4",
"@angular/core": "^4.2.4",
"@angular/forms": "^4.2.4",
"@angular/http": "^4.2.4",
"@angular/platform-browser": "^4.2.4",
"@angular/platform-browser-dynamic": "^4.2.4",
"@angular/router": "^4.2.4",
"angular-svg-round-progressbar": "^1.1.0",
"bulma": "^0.5.3",
"core-js": "^2.4.1",
"rxjs": "^5.4.2",
"zone.js": "^0.8.14"
},
"devDependencies": {
"@angular/cli": "1.4.1",
"@angular/compiler-cli": "^4.2.4",
"@angular/language-service": "^4.2.4",
"@types/jasmine": "~2.5.53",
"@types/jasminewd2": "~2.0.2",
"@types/node": "~6.0.60",
"codelyzer": "~3.1.1",
"electron": "^1.7.6",
"electron-packager": "^9.1.0",
"jasmine-core": "~2.6.2",
"jasmine-spec-reporter": "~4.1.0",
"karma": "~1.7.0",
"karma-chrome-launcher": "~2.1.1",
"karma-cli": "~1.0.1",
"karma-coverage-istanbul-reporter": "^1.2.1",
"karma-jasmine": "~1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.1.2",
"ts-node": "~3.2.0",
"tslint": "~5.3.2",
"typescript": "~2.3.3"
}
}

Electron

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

let win;

function createWindow () {
// Create the browser window.
win = new BrowserWindow({
width: 600,
height: 600,
backgroundColor: '#ffffff',
icon: `file://${__dirname}/dist/assets/logo.png`
})

win.loadURL(`file://${__dirname}/dist/index.html`)

//// uncomment below to open the DevTools.
// win.webContents.openDevTools()

// Event when the window is closed.
win.on('closed', function () {
win = null
})
}

// Create window on electron intialization
app.on('ready', createWindow)

// Quit when all windows are closed.
app.on('window-all-closed', function () {

// On macOS specific close process
if (process.platform !== 'darwin') {
app.quit()
}
})

app.on('activate', function () {
// macOS specific close process
if (win === null) {
createWindow()
}
})

谢谢。

目前,我每次都使用以下命令构建我的应用程序

ng build --prod && electron .

这很累,所以我希望能够构建运行一个命令,以便它监视更改和实时重新加载。

最佳答案

您可以使用 electron-reload用于热模块重新加载。它监听文件更改并重新加载 Electron 应用程序。

只需将它添加到 package.json 中的 devDependencies。

然后,您必须将它添加到您的 main.ts 中:

import { app, BrowserWindow, Menu } from 'electron';

let win, serve;
const args = process.argv.slice(1);
serve = args.some(val => val === '--serve');

if (serve) {
require('electron-reload')(__dirname, {});
}

然后在你的json包中添加命令

"start": "webpack --watch",
"serve": "npm run build:main && electron ./dist --serve",
"build:main": "tsc main.ts --outDir dist && copyfiles package.json dist && cd dist && npm install --prod"

build:main 是编译项目的构建脚本。这会编译所有 Typescript 文件并将它们放入 dist 文件夹中。然后,它运行 npm install 从 NPM 下载和安装模块。

您需要模块 bundler Webpack 才能运行。安装它

npm install --save-dev webpack

首先,在控制台中运行 npm start。然后,在第二个控制台中执行npm run serve。现在,它会监听更改并根据文件更改重新编译。

tsc 代表 TypeScript 编译器。如果您将 tsc 用作节点模块,请确保已安装它:

npm install -g typescript

我目前将它用于具有相同设置(Angular 4、Electron)的项目,并且它运行良好。

关于Angular 4 + Electron - 如何运行应用程序并观察变化(实时重新加载),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46533095/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com