electron入門教程


electron 使用 JavaScript, HTML 和 CSS 構建跨平台的桌面應用

先說下結構如下:

 

1、首先安裝node       下載地址:https://nodejs.org/zh-cn/

2、新建文件夾demo1

3、使用終端進入文件夾  win下在 開始--cmd

4、初始化項目

npm init -f              // -f它會使用默認只,而不是提示您輸入的任何選項。

項目文件夾下會多出package.json文件

5、打開package.json

main:'index.js'修改為  main:'main.js'

6、install electron

npm install electron --save  //也可用 cnpm install electron --save

package.json 會加入electron

7、項目根目錄下新建main.js

const electron = require('electron')
const path = require("path")
const url = require('url')

const { app,BrowserWindow,globalShortcut} = require('electron')

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(url.format({
    pathname: path.join(__dirname, 'index.html'),
    protocol: 'file:',
    slashes: true
  }))

  // 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.

8、項目根目錄下創建index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Document</title>
</head>
<body>
    <h1>hello world</h1>
</body>
</html>

9、修改package.json 

{
  "name": "demo1",
  "version": "1.0.0",
  "description": "",
  "main": "main.js",
  "scripts": {
    "start": "electron ."
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "electron": "^1.7.10"
  }
}

10、在項目跟目錄下 終端執行 npm start

如果下面頁面打開說明成功

11、打包

打開項目路徑

1使用命令 npm install --save-dev electron-packager將electron-package安裝到項目的路徑下面

注:完成以上兩步驟會在 package.json 生成文件

"devDependencies": {
  "electron-packager": "^8.5.1"
}

3在項目根目錄下面的 package.json 里添加類似於如下代碼

復制代碼
 
         

"scripts": {

 
         

os系統:"packageDarwin": "electron-packager . 'Hosts' --platform=darwin --arch=x64 --icon=hosts.icns --out=./dist --asar --app-version=2.0.1 --ignore=\"(dist|src|docs|.gitignore|LICENSE|README.md|webpack.config*|node_modules)\"",

os系統:"packageDarwin": "electron-packager . 'Hosts' --platform=darwin --arch=x64 --icon=hosts.icns --out=./dist --asar --app-version=2.0.1",


windows系統:"packageWin": "electron-packager . 'Hosts' --platform=win32 --arch=x64 --icon=hosts.ico --out=./dist --asar --app-version=2.0.1 --ignore=\"(dist|src|docs|.gitignore|LICENSE|README.md|webpack.config.js|node_modules)\"",

windows系統:"packageWin": "electron-packager . 'Hosts' --platform=win32 --arch=x64 --icon=hosts.ico --out=./dist --asar --app-version=2.0.1",


linux系統:"packageLinux": "electron-packager . 'Hosts' --platform=linux --arch=x64 --out=./dist --asar --app-version=2.0.1 --ignore=\"(dist|src|docs|.gitignore|LICENSE|README.md|webpack.config.js|node_modules)\""

linux系統:"packageLinux": "electron-packager . 'Hosts' --platform=linux --arch=x64 --out=./dist --asar --app-version=2.0.1"

 
         

}

復制代碼

 

命令說明: 
* location of project:項目所在路徑 
* name of project:打包的項目名字 
* platform:確定了你要構建哪個平台的應用(Windows、Mac 還是 Linux) 
* architecture:決定了使用 x86 還是 x64 還是兩個架構都用 
* electron version:electron-prebuilt 的版本 
* optional options:可選選項

PS:這里要注意,字段里的 項目名字,version,icon路徑要改成自己的; 例如:"packager": "electron-packager ~/Desktop/myFirstElectronApp(項目位置) Hello(項目名稱) --linux --out ./OutApp(項目導出位置) --version 1.4.13 --overwrite"

4然后,使用命令 npm run-script package---即可打包

 

注:如果你是mac可以參考我的

{
  "name": "demo1",
  "version": "1.0.0",
  "description": "",
  "main": "main.js",
  "scripts": {
    "start": "electron .",
    "packageDarwin": "electron-packager . 'Hosts' --platform=darwin --arch=x64  --out=./dist --asar --app-version=2.0.1 --ignore=\"(dist|src|docs|.gitignore|LICENSE|README.md|webpack.config*|node_modules)\""
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "electron": "^1.7.10",
  "electron-packager": "^10.1.0" } }

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM