发布于 2026-01-06 8 阅读
0

利用 Angular 构建桌面应用程序 我们将要做什么?!! 设置 Angular 设置 Electron 从 Angular 应用程序内部访问 Electron API。 总结

利用 Angular 的强大功能构建桌面应用程序

我们该怎么办?!

设置 Angular

设置 Electron

从 Angular 应用程序内部访问 Electron API。

结论

角电子

如果你会建网站,你就能建桌面应用。

如果你会写 JavaScript 代码,确实可以构建一个外观和行为都与原生应用无异的桌面应用。不仅如此,你还可以将 Angular 的强大功能引入桌面应用领域,让你的用户界面 (IU) 看起来惊艳无比 😍。好了,不多说了,去泡杯咖啡,我们开始吧!

我们该怎么办?!

我们将使用ElectronAngular构建一个基本的桌面应用程序。

在开始之前,我假设您已经具备Node.js和Angular的一些基本知识。

设置 Angular

如果您尚未安装 Angular CLI,请运行以下命令进行安装。

$ npm install -g @angular/cli
Enter fullscreen mode Exit fullscreen mode

现在让我们创建一个新的Angular应用程序。

$ ng new angular-electron
Enter fullscreen mode Exit fullscreen mode

它会询问你想使用哪个样式编译器,以及是否要使用 Angular 路由器等等,这些配置完全无关紧要,选择你想要的任何选项即可。

您现在可以通过运行以下程序来查看您的应用程序的实际运行效果……

$ cd angular-electron
$ ng serve
Enter fullscreen mode Exit fullscreen mode

然后打开浏览器访问http://localhost:4200/,不过这还不是最有趣的部分,我们继续往下看。

我们需要修改项目 src 文件夹中的 index.html 文件,在 base 标签中添加一个句点,以便我们的应用程序可以找到静态文件,不要跳过这一步,这非常重要。

<base href="./">
Enter fullscreen mode Exit fullscreen mode

设置 Electron

现在我们要把 Electron 添加到我们的应用程序中。

$ npm install --save-dev electron
Enter fullscreen mode Exit fullscreen mode

我们还需要一些依赖项。

$ npm install --save-dev app-root-path
Enter fullscreen mode Exit fullscreen mode

现在让我们为 Electron 应用程序创建一个新文件夹。

$ mkdir bin && cd bin && touch main.ts
Enter fullscreen mode Exit fullscreen mode

如您所见,我们创建了一个 bin 文件夹main.ts,其中包含一个文件。之所以将主文件创建为带有 .tsts扩展名而不是 .ts 扩展名,js是因为我们已经在 Angular 应用程序中使用 TypeScript,那么为什么不在整个项目中都使用 TypeScript 呢?

现在让我们往main.ts文件中添加一些代码(终于要写代码了😅)

import { app, BrowserWindow } from 'electron';
import { resolve } from 'app-root-path';

// 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: BrowserWindow;

function createWindow () {
  // Create the browser window.
  win = new BrowserWindow({
    width: 800,
    height: 600
  });

  // Load the angular app.
  // Make sure that this path targets the index.html of the
  // angular application (the distribution).
  win.loadFile(resolve('dist/angular-electron/index.html'));

  // Emitted when the window is closed.
  win.on('closed', () => {
    // Dereference 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;
  });
}

// This method will be called when the 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();
  }
});

// In this file, you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
Enter fullscreen mode Exit fullscreen mode

上面的代码与官方网站上提到的完全相同,但使用的是 Typescript 语法,还要注意win.loadFile链接到 Angular 应用程序入口文件的函数“我们还没有构建它”。

好吧,我们需要检查一下我们这样做是否真的有效,对吧!

让我们在package.json文件中添加一个脚本,以便构建和运行这个应用程序。

"main" : "bin/main.js",
"scripts": {
 ...
   “electron”: “tsc bin/main.ts && ng build && electron bin/main.js”
}
Enter fullscreen mode Exit fullscreen mode

现在让我们来看看它的实际效果。

$ npm run electron
Enter fullscreen mode Exit fullscreen mode

目前你应该可以看到应用程序已经启动并运行,其中包含 Angular 标志,到目前为止一切顺利 😉。

好了,现在我们的应用程序已经运行了,但是我们可以在Angular应用程序内部使用Electron API吗?!!

别慌,其实很简单,只要跑就行了……

$ npm install --save-dev ngx-electron
Enter fullscreen mode Exit fullscreen mode

从 Angular 应用程序内部访问 Electron API。

我们刚刚安装了ngx-electron,它将使我们的工作轻松许多,那么让我们看看如何使用它。

我们需要像在 Angular 文件中使用其他模块一样导入这个模块app.module.ts

import { NgxElectronModule } from 'ngx-electron';
@NgModule({
imports: [
  ...
  NgxElectronModule
]
})
export class AppModule {}
Enter fullscreen mode Exit fullscreen mode

好了,现在我们可以把它用在我们的组件里了,比如……

import { ElectronService } from 'ngx-electron';
export class AppComponent {
   constructor(private _electronService: ElectronService) {
   // now we have access to electron api through this service
   }
}
Enter fullscreen mode Exit fullscreen mode

我们来看看是否真的可以访问 Electron API。

请将文件中的内容替换app.component.ts为以下内容。

import { Component } from '@angular/core';
import { ElectronService } from 'ngx-electron';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  title = 'angular-electron';

  versions = { node: '', chrome: '', electron: '' };

  constructor(private _electronService: ElectronService) {
    // Make sure that app is being executed inside of electron.
    if (this._electronService.isElectronApp) {
      // We have access to node process.
      this.versions.node = this._electronService.process.versions.node;
      this.versions.chrome = this._electronService.process.versions.chrome;
      this.versions.electron = this._electronService.process.versions.electron;
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

并将文件内容替换app.component.html为以下内容。

<!--The content below is only a placeholder and can be replaced.-->

<div style="text-align:center">

  <h1>Welcome to {{ title }}!</h1>

  <ul>
    <li>Node version {{ versions.node }}</li>
    <li>Chrome version {{ versions.chrome }}</li>
    <li>Electron version {{ versions.electron }}</li>
  </ul>

</div>
Enter fullscreen mode Exit fullscreen mode

所以,你觉得怎么样?别想太多,让我们看看实际效果吧😅。

$ npm run electron
Enter fullscreen mode Exit fullscreen mode

现在,你应该可以看到应用程序已经运行起来了,使用了我们所使用的代码、Chrome 和 Electron 版本,嗯,我们成功了 😉。

结论

开发桌面应用程序并不难,我们可以使用像 Node、Angular、Electron 和 TypeScript 这样强大的工具来完成出色的工作。你知道吗?即使你是 Web 开发人员,在空闲时间尝试一些新技术也不错,我确信你会从尝试新技术中学到一些新东西 😉。

如果我有空的话,我可能会写一篇新文章,介绍如何为我们的应用程序编写一些单元测试。

如果遇到问题,您可以随时参考此应用程序的Github 代码库。

本文最初发表于Medium。

文章来源:https://dev.to/ahmedmkamal/build-desktop-apps-with-the-power-of-angular-18g7