随着 JavaScript 开源社区的发展,JavaScript 的应用场景越来越广泛,到目前为止,JavaScript 在Web开发、桌面开发、APP开发、服务端开发方面都可以胜任。
在桌面开发方面,如果你熟悉 C#或 Java 等技术,你就知道构建桌面应用程序的复杂,有时可能需要自己编写 DLL(动态链接库)文件。而 Electron 则基于 HTML、CSS、JavaScript,并且是跨平台的。目前有很多优秀的桌面软件都是使用 Electron 开发的:
微软的 VSCode 也是基于 Electron 开发的,居然没用自己的开发套件,可见 Electron 的桌面解决方案的优秀。
What We Will Build
We will be building a desktop application that allows people to take quiz questions, and at the end of the questions, see their total score.
Getting Started With The Vue-Cli
To get started easily and also skip the process of configuring Webpack for the compilation from ES2016 to ES15, we will use the Vue CLI.
vue init webpack electron-vue
//change directory into the folder
cd electron-vue
//install the npm dependencies
npm install
After installing these modules, we need to install Electron on our system.
Installing And Configuring Electron
I usually prefer running Electron globally, but you can install locally.
To install globally, we can run:
npm install -g electron
To install locally, we can run:
npm install electron
Let's move into our package.json
file which has been created by our vue-cli
and add a new line to it under the first object block, which defines name, version, description, author, private. Just before the scripts also, we would add a key pair called "main"
with a value of "elect.js"
.
"name": "electron-vue",
"version": "1.0.0",
"description": "A Vue.js project",
"author": "zhangyongjie <[email protected]>",
"private": true,
"main": "elect.js",
"scripts": {
"dev": "node build/dev-server.js",
"start": "node build/dev-server.js",
"build": "node build/build.js"
},
We have defined elect.js
as the starting point for Electron to run its application, so in our root folder, we will create a file called elect.js
and put in the following content:
const {
app,
BrowserWindow
} = require('electron')
let win = null;
app.on('ready', function () {
// Initialize the window to our specified dimensions
win = new BrowserWindow({
width: 1000,
height: 600
});
// Specify entry point to default entry point of vue.js
win.loadURL('http://localhost:8080');
// Remove window once app is closed
win.on('closed', function () {
win = null;
});
});
//create the application window if the window variable is null
app.on('activate', () => {
if (win === null) {
createWindow()
}
});
//quit the app once closed
app.on('window-all-closed', function () {
if (process.platform != 'darwin') {
app.quit();
}
});
The main focus here is the ready
event, where we set a new dimension for our app, load the entry point, which could be a direct URL or a file URL. But for now, we are loading the development URL for Vue.
At this point, we can open up two terminals to the root of our project.
In the first terminal, we want to serve our Vue application, So we run:
npm run dev
On the second terminal, we want to run the Electron application, so we run:
electron .
If all goes well, we should be seeing this:
Creating The Quiz Application
It's time to move into our src/App.vue
to do the main quiz application.
{{ quiz.category }}
{{ quiz.question }}
-
Your Total score is {{score}} / {{quizez.length}}
Packaging Electron To Use The Production Ready App
Right now, the application still runs from the development server. It is now time to run the app from the file directly, to be packaged with Electron.
On the terminal running the Vue server, hit ctrl+c
, then run the following command:
npm run build
After running this, a file would be created in your root folder, under dist/index.html
. Open up the file, remove all leading /
from all references to JS or CSS files, then close. If we don't do this, our application would load only an empty screen, as it would not be able to locate our CSS and JavaScript files.
At this point, let's go back into our elect.js
, and add some configurations to make it serve from the file. At the top of the file, add these two imports:
const url = require('url')
const path = require('path')
Lets replace the part that says win.loadURL('http://localhost:8080')
with the code below:
win.loadURL(url.format({
pathname: path.join(__dirname, 'dist/index.html'),
protocol: 'file:',
slashes: true
}));
What we have done here is to tell Electron to load the index.html in our dist folder, and it should attempt to load it as a file rather than an actual url.
Once done, save and hit electron .
At this point, we have our app running as seen below: