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

相关文章
相关标签/搜索