Electron的第一个应用

目前一个项目的包括移动端(,IOS)、web端还有桌面应用 , 作为一个会Html+css的开发人员来说 , 使用作为前台的框架可以完美的解决web端和移动端的问题 , 但是对于桌面应用 , 心里有点不甘 , 因为想做出一个简单的客户端 , 你要么使用java的Swing编程 , 要么会使用MFC等等 , 这样学习的代价太高 , 也不便维护 , 于是解决了这一问题 , 它可以将你做的web界面直接移植到桌面应用里 。像现在比较火的钉订客户端就是使用这种方式开发 , 当我知道钉订客户端是使用html+css开发的时候 , 我的表情是酱紫的

Electron的第一个应用

文章插图
当然了它是使用nw.js并不是,我们项目以后要使用来开发桌面应用 , 那么就赶快制作我的第一个---‘Hello world’;
框架的前身是 Atom Shell , 可以让你写使用  , HTML 和 CSS 构建跨平台的桌面应用程序 。它是基于io.js 和开源项目 , 并用于在 Atom 编辑器中 。是开源的 , 由维护 , 有一个活跃的社区 。最重要的是 ,  应用服务构建和运行在 Mac ,  和 Linux 。
基于本人的情况 , 推荐下载 来完成环境的支持 , 点击下载
Electron的第一个应用

文章插图
也许你会下载的很慢很慢 , 分享一个百度云网盘
Electron的第一个应用

文章插图
下载解压 , 打开目录 , 运行.exe , 弹出界面
Electron的第一个应用

文章插图
环境什么的我们就不用担心了 , 如果你执意想通过npm来下载依赖包并运行的 , 会出现意想不到的错误 , 什么错误?亲自去跳!
现在来跑第一个应用
首先开看下他的目录结构
Electron的第一个应用

文章插图
index.html---需要显示的页面 , 里面输出“hello world"
Electron的第一个应用

文章插图
Hello World!Hello World!We are using node ,Chromium ,and Electron .
main.js---负责界面的启动与一些设置 , 比如工具栏啊 , 主题啦等等
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 mainWindowfunction 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 + Qif (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.