1. 程式人生 > >Electron的安裝與 hello word

Electron的安裝與 hello word

led load bsp node nis 1.0 nbsp multi lose

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>
View Code

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.
View Code

Electron的安裝與 hello word