目前一个项目的Client包括移动端(Android,IOS)、web端还有桌面应用,作为一个会Html+css的开发人员来说,使用bootstrap作为前台的框架可以完美的解决web端和移动端的问题,但是对于桌面应用,心里有点不甘,因为想做出一个简单的客户端,你要么使用java的Swing编程,要么会使用MFC等等,这样学习的代价太高,也不便维护,于是Electron解决了这一问题,它可以将你做的web界面直接移植到桌面应用里。像现在比较火的钉订客户端就是使用这种方式开发,当我知道钉订客户端是使用html+css开发的时候,我的表情是酱紫的
当然了它是使用nw.js并不是electron,我们项目以后要使用Electron来开发桌面应用,那么就赶快制作我的第一个application---‘Hello world’;
Electron 框架的前身是 Atom Shell,可以让你写使用 JavaScript,HTML 和 CSS 构建跨平台的桌面应用程序。它是基于io.js 和 Chromium 开源项目,并用于在 Atom 编辑器中。Electron 是开源的,由 GitHub 维护,有一个活跃的社区。最重要的是,Electron 应用服务构建和运行在 Mac,Windows 和 Linux。
基于本人的情况,推荐下载Electron Releases来完成环境的支持,点击下载
也许你会下载的很慢很慢,分享一个百度云网盘http://pan.baidu.com/s/1b31GdC
下载解压,打开目录,运行electron.exe,弹出界面
环境什么的我们就不用担心了,如果你执意想通过npm来下载依赖包并运行的,会出现意想不到的错误,什么错误?亲自去跳!
现在来跑第一个应用
首先开看下他的目录结构
index.html---需要显示的页面,里面输出“hello world"
<span style="font-family:Microsoft YaHei;font-size:14px;"><!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
<!-- All of the Node.js APIs are available in this renderer process. -->
We are using node <script>document.write(process.versions.node)</script>,
Chromium <script>document.write(process.versions.chrome)</script>,
and Electron <script>document.write(process.versions.electron)</script>.
</body>
</html>
</span>
main.js---负责Electron界面的启动与一些设置,比如工具栏啊,主题啦等等
<span style="font-family:Microsoft YaHei;font-size:14px;">const electron = require('electron')
// Module to control application life.
const app = electron.app
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow
// 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 mainWindow
function createWindow () {
// Create the browser window.
mainWindow = new BrowserWindow({width: 800, height: 600})
// and load the index.html of the app.
mainWindow.loadURL(`file://${__dirname}/index.html`)
// Open the DevTools.
//mainWindow.webContents.openDevTools()
// Emitted when the window is closed.
mainWindow.on('closed', function () {
// 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.
mainWindow = null
})
}
// This method will be called when 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', function () {
// On OS X 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', function () {
// On OS X 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 (mainWindow === 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.
</span>
package.json---负责app的版本声明
<span style="font-family:Microsoft YaHei;font-size:14px;">{
"name": "ElectronApplication",
"version": "1.0.0",
"description": "A minimal Electron application",
"main": "main.js"
}
</span><span style="font-family:Microsoft YaHei;font-size:14px;">
</span>
将项目直接拖拉到Electron.exe界面里,应用就跑了起来
强烈推荐使用这种方式,说多了都是泪啊!