Electron入手記錄(原有Vue項目中添加Electron)

前言

這是開發三端的一個框架,Win + Linux + MAChtml

入手

秉持一向風格,上代碼
首先package.json添加一些東西,參照google的方法添加註釋vue

"main": "main.js",
  "build": {
    "//": "",
    "appId": "Flands",
    "copyright": "Open Totally",
    "compression": "normal",
    "//": "不知道幹嗎的,nsis是微軟的一個打包程序,自行搜索",
    "nsis": {
      "oneClick": true,
      "perMachine": true,
      "runAfterFinish": true
    },
    "//": "關鍵點,這是要打包進去的文件,就資源+html+main.js",
    "//": "由於我用的vue,編譯後是這些東西,添加就好",
    "files": [
      "dist/static",
      "dist/*.html",
      "*.js"
    ]
  },
  "scripts": {
    "dev": "node build/dev-server.js",
    "build": "node build/build.js",
    "//": "分別是調試運行,打包exe運行,打包成單個exe文件",
    "//": "打包在dist/win-unpacked裏,有個exe文件就是",
    "start": "electron .",
    "pack": "electron-builder --dir",
    "dist": "electron-builder"
  },
  "devDependencies": {
    "//": "添加的框架,注意是dev裏,builder是打包的工具,packager是builder的依賴",
    "electron": "^1.7.9",
    "electron-builder": "^19.48.2",
    "electron-packager": "^10.1.0",
  }

根目錄建立main.js,這是配置文件node

const electron = require('electron')
// Module to control application life.
const app = electron.app
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow
// const newWindowBtn = document.getElementById('frameless-window')

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 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, './dist', 'index.html'),
    protocol: 'file:',
    slashes: true
  }))

  // 自動打開調試臺
  mainWindow.webContents.openDevTools({
    detach: 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.

運行

參考package裏添加的腳本,三選一
npm run [start] [pack] [dist]webpack

熱模塊替換

  • webpack自帶的熱模塊更新功能
  • main.js裏寫入 mainWindow.loadURL('http://localhost:8090/' + '#')便可
  • 地址填本身的端口號

使用Electron框架的功能

在須要的vue頁面中添加web

if (window.require) {
    var ipc = window.require('electron').ipcRenderer
  }

方法裏添加vue-router

if (window.require) {
    ipc.send('close');
  }

緣由:直接require會致使提示找不到fs模塊,須要使用window.require,可是在Chrome環境中提示window.require not function因此須要作一次判斷
在Electron的main.js裏添加npm

const ipc = require('electron').ipcMain
ipc.on('close', e => mainWindow.close());

注意事項

  • 使用vue-router的話必須用Hash模式,不能使用history
相關文章
相關標籤/搜索