Nodejs中文網下載下載安裝包:http://nodejs.cn/download/html
一系列下一步以後,安裝完成,自帶管理工具NPM.能夠經過它下載一系列模塊。node
安裝Electron的過程比較坎坷,嘗試了三次才成功。web
1.直接cmd 敲命令chrome
1 npm install electron -g
,卡了一會,提示超時,安裝失敗.npm
2.在網上搜了一下,有人說由於服務器在國外,須要換源。好吧,那就安裝一個Nrm 用來切換Npm的數據源,敲命令 json
npm install nrm -g
結果跟安裝electron同樣,提示超時,安裝失敗。windows
3.繼續百度,發現淘寶有個cnpm能夠用,抱着試試看的心態,輸入命令服務器
npm install -g cnpm --registry=https://registry.npm.taobao.org cnpm install -g electron
OK ,安裝成功!app
1.新建一個項目文件夾 testelectron
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" }
<!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>
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.