1.NodeJs安装
Nodejs中文网下载下载安装包:http://nodejs.cn/download/
一系列下一步之后,安装完成,自带管理工具NPM.可以通过它下载一系列模块。
2.Electron安装
安装Electron的过程比较坎坷,尝试了三次才成功。
1.直接cmd 敲命令
1 npm install electron -g
,卡了一会,提示超时,安装失败.
2.在网上搜了一下,有人说因为服务器在国外,需要换源。好吧,那就安装一个Nrm 用来切换Npm的数据源,敲命令
npm install nrm -g
结果跟安装electron一样,提示超时,安装失败。
3.继续百度,发现淘宝有个cnpm可以用,抱着试试看的心态,输入命令
npm install -g cnpm --registry=https://registry.npm.taobao.org cnpm install -g electron
OK ,安装成功!
3.Electron hello word
1.新建一个项目文件夹 test
2.在文件夹中新建三个文件
package.json
main.js
index.html
package.json:
文件名是固定的,启动项目时,第一个找的就是它,里面定义了主进程的文件路径,我们这里的主进程是main.js.
main.js :
里面创建electron窗口,以及初始化的操作.将html页面加载到窗口中,我们这里的页面是index.html
index.html:
就是html页面
介绍完毕,下面开始贴代码
package.json
{ "name" : "your-app", "version" : "0.1.0", "main" : "main.js" }
index.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Hello World!</title> </head> <body> <h1>Hello World!</h1> We are using node <script>document.write(process.versions.node)</script>, Chrome <script>document.write(process.versions.chrome)</script>, and Electron <script>document.write(process.versions.electron)</script>. </body> </html>
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 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.
浙公网安备 33010602011771号