windows下用Electron、html5開發桌面應用

準備工做

安裝nodejs、npm這些是必須的,這些不細說。javascript

安裝Electron

因爲國外服務器太慢,須要走國內服務器進行安裝,因此先安裝淘寶鏡像
npm install cnpm -g --registry=http://registry.npm.taobao.org
而後再執行
cnpm install electron -g
這樣就完成了electron的安裝css

安裝Electron的打包工具electron-packager

npm install electron-packager -ghtml

建立app

首先,將html、css、js等資源文件放到某個目錄,好比myApp,而後在好比myApp目錄建立package.json和main.js文件.java

配置package.json和main.js

package.json:node

{
  "name"    : "yourAppName",
  "version" : "1.0.14",
  "main"    : "main.js"
}

main.js:jquery

const {app, BrowserWindow, Menu} = require('electron')
const path = require('path')
const url = require('url')

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

function createWindow () {
  // Create the browser window.
  win = new BrowserWindow({width: 1092, height: 690})

  // and load the index.html of the app.
  win.loadURL(url.format({
    pathname: path.join(__dirname, 'index.html'),
    protocol: 'file:',
    slashes: true
  }))

  // Open the DevTools.
  // win.webContents.openDevTools()

  // Emitted when the window is closed.
  win.on('closed', () => {
    // 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.
    win = 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', () => {
  // On macOS 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', () => {
  // On macOS 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 (win === 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.

const template = [
  {
    label: 'View',
    submenu: [
      {
        label: 'Dev tools',
        click(){win.webContents.openDevTools();}
      },
    ]
  },
  {
    role: 'window',
    submenu: [
      {role: 'minimize'},
      {role: 'close'}
    ]
  },
  {
    role: 'help',
    submenu: [
      {
        label: 'Learn More',
        click () { require('electron').shell.openExternal('https://electron.atom.io') }
      }
    ]
  }
]

if (process.platform === 'darwin') {
  template.unshift({
    label: app.getName(),
    submenu: [
      {role: 'about'},
      {type: 'separator'},
      {role: 'services', submenu: []},
      {type: 'separator'},
      {role: 'hide'},
      {role: 'hideothers'},
      {role: 'unhide'},
      {type: 'separator'},
      {role: 'quit'}
    ]
  })

  // Edit menu
  template[1].submenu.push(
    {type: 'separator'},
    {
      label: 'Speech',
      submenu: [
        {role: 'startspeaking'},
        {role: 'stopspeaking'}
      ]
    }
  )

  // Window menu
  template[3].submenu = [
    {role: 'close'},
    {role: 'minimize'},
    {role: 'zoom'},
    {type: 'separator'},
    {role: 'front'}
  ]
}

const menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu)

main.js裏面加入了menu,在menu裏面加入了openDevTools()方法,這樣就能夠在運行的時候,經過菜單欄打開web dev tools進行頁面調試了。web

頁面引用js的地方須要修改一下

Electron 的 Renderer 端由於注入了 Node 環境,存在全局函數 require,致使 jQuery 內部環境判斷有問題,會報$ is not defined的錯誤。能夠經過下面的方式解決。在引用js資源的時候,在前面和後面包一層javascript。shell

<!-- Insert this line above script imports  -->
<script>if (typeof module === 'object') {window.module = module; module = undefined;}</script>

<!-- normal script imports etc  -->
<script src="scripts/jquery.min.js"></script>    
<script src="scripts/vendor.js"></script>    

<!-- Insert this line after script imports -->
<script>if (window.module) module = window.module;</script>

https://stackoverflow.com/questions/32621988/electron-jquery-is-not-definednpm

打包

完成上面的步驟以後,能夠進行打包了。
在該目錄按住shift鍵而且單擊鼠標右鍵,打開命令行,而後運行下面命令進行打包json

electron-packager . myAppName --win --out release --arch=x64 --electronVersion 1.6.8 --overwrite --ignore=node_modules

說明:
electron-packager . 可執行文件的文件名 --win --out 打包成的文件夾名 --arch=x64位仍是32位 --electronVersion electron的版本號 --overwrite --ignore=node_modules
在本例中,打包成功後,會在myApp目錄生成一個release目錄,裏面是生成的打包文件。

運行

在release目錄找到myAppName.exe運行便可

調試

能夠在main.js裏面直接運行 win.webContents.openDevTools(),這樣程序執行以後,會自動打開dev tools; 或者像本例中的main.js這樣,添加一個menu,在menu裏面加入了dev tools菜單,點擊打開調試。


參考資料
1.https://electron.atom.io/docs/api/menu/
2.http://blog.csdn.net/a727911438/article/details/70834467?utm_source=itdadao&utm_medium=referral
3.http://blog.csdn.net/a727911438/article/details/70834467?utm_source=itdadao&utm_medium=referral 4.https://stackoverflow.com/questions/32621988/electron-jquery-is-not-defined
5.https://cnodejs.org/topic/571f2b55fa48138c41110e1a

相關文章
相關標籤/搜索