API 浏览器
Quasar CLI with Vite - @quasar/app-vite
Electron 准备

在开始实际开发之前,我们需要做一些准备工作。

¥Before we dive in to the actual development, we need to do some preparation work.

步骤 1:添加 Quasar Electron 模式(Step 1: Add Quasar Electron Mode)

¥Step 1: Add Quasar Electron Mode

为了开发/构建 Quasar Electron 应用时,我们需要将 Electron 模式添加到 Quasar 项目中。它的作用是使用 yarn/npm/pnpm/bun 安装一些 Electron 包并创建 /src-electron 文件夹。

¥In order to develop/build a Quasar Electron app, we need to add the Electron mode to our Quasar project. What this does is that it yarn/npm/pnpm/bun installs some Electron packages and creates /src-electron folder.

$ quasar mode add electron

每个 Electron 应用都有两个线程:主线程(处理窗口和初始化代码 - 来自新创建的文件夹 /src-electron)和渲染线程(处理来自 /src 的应用实际内容)。

¥Every Electron app has two threads: the main thread (deals with the window and initialization code – from the newly created folder /src-electron) and the renderer thread (which deals with the actual content of your app from /src).

新文件夹的结构如下:

¥The new folder has the following structure:

icon.icns
# Icon file for Darwin (MacOS) platform
icon.ico
# Icon file for win32 (Windows) platform
icon.png
# Tray icon file for all platforms
electron-preload.js
# (or .ts) Electron preload script (injects Node.js stuff into renderer thread)
electron-main.js
# (or .ts) Main thread code
electron-env.d.ts
# TypeScript only

Windows 用户须知(A note for Windows Users)

¥A note for Windows Users

如果你在 npm install 过程中遇到有关 node-gyp 的错误,那么你很可能没有在系统上安装正确的构建工具。构建工具包括 Python 和 Visual Studio 等。幸运的是,有几个软件包可以帮助简化此过程。

¥If you run into errors during npm install about node-gyp, then you most likely do not have the proper build tools installed on your system. Build tools include items like Python and Visual Studio. Fortunately, there are a few packages to help simplify this process.

我们需要检查的第一项是我们的 npm 版本,并确保它没有过时。这是使用 npm-windows-upgrade 实现的。如果你使用 yarn,则可以跳过此检查。

¥The first item we need to check is our npm version and ensure that it is not outdated. This is accomplished using npm-windows-upgrade. If you are using yarn, then you can skip this check.

完成后,我们可以继续设置所需的构建工具。使用 windows-build-tools,大部分繁琐的工作都已为我们完成。全局安装此依赖将依次设置 ​​Visual C++ 包、Python 等。

¥Once that is complete, we can then continue to setup the needed build tools. Using windows-build-tools, most of the dirty work is done for us. Installing this globally will in turn setup Visual C++ packages, Python, and more.

注意:2019 年 4 月

在 Powershell.exe(以管理员身份运行)中,npm install --global windows-build-tools 目前似乎失败,错误指向 python2 和 vctools。你可以使用 Chocolatey 解决这个问题。一行安装:

¥In Powershell.exe (Run as Admin) npm install --global windows-build-tools seems to fail at the moment with errors pointing to python2 and vctools. You can get around this with Chocolatey. One-liner install:

Set-ExecutionPolicy Bypass -Scope Process -Force;iex ((New-Object System.Net.WebClient).DownloadString(‘https://chocolatey.org/install.ps1’))

然后运行 ​​choco upgrade python2 visualstudio2017-workload-vctools

¥and then run choco upgrade python2 visualstudio2017-workload-vctools.

此时,所有程序应该可以成功安装,但如果没有,则需要全新安装 Visual Studio。请注意,这些不是 Quasar 的问题,而是与 NPM 和 Windows 有关。

¥At this point things should successfully install, but if not then you will need a clean installation of Visual Studio. Please note that these are not problems with Quasar, but they are related to NPM and Windows.

步骤 2:开始开发(Step 2: Start Developing)

¥Step 2: Start Developing

如果你想直接开始开发,可以跳过上一步,直接执行 “quasar 模式” 命令并执行:

¥If you want to jump right in and start developing, you can skip the previous step with “quasar mode” command and issue:

$ quasar dev -m electron

# passing extra parameters and/or options to
# underlying "electron" executable:
$ quasar dev -m electron -- --no-sandbox --disable-setuid-sandbox
# when on Windows and using Powershell:
$ quasar dev -m electron '--' --no-sandbox --disable-setuid-sandbox

如果缺少 Electron 模式,这将自动添加它。它会打开一个 Electron 窗口,其中会渲染你的应用,并同时打开开发者工具。

¥This will add Electron mode automatically, if it is missing. It will open up an Electron window which will render your app along with Developer Tools opened side by side.