手把手教你 Electron + Vue 搭建前端桌面應用

1、在使用 Electron 以前咱們要了解 Electron 是什麼?

Electron 官網地址 點此 : electron 官方地址javascript

Electron 至關於一個瀏覽器的外殼 , 咱們將 編寫的 HTML , CSS , Javascript 網頁程序 嵌入進 Electron 裏面html

以便於在桌面上進行運行。 通俗來說它就是一個軟件 , 如 QQ 、網易雲音樂、優酷視頻 等等。功能至強大vue

超乎你的想象java

2、開發環境的準備

  1. 安裝 Node.js 壞境node

    Node.js 官網地址 點此 node.js 官方地址git

  2. 安裝 Vue Cligithub

    npm install -g @vue/cli
    複製代碼
  3. 安裝 淘寶鏡像 cnpm ( 因 electron 包 特別大 本人嘗試過 npm 或 yarn 的 方法 須要等待很長時間 )web

  4. 全局安裝 Electronvue-cli

    cnpm install electron -g
    
    //若是安裝仍是 特比慢 或 不想安裝cnpn 能夠聯繫咱們
    複製代碼

​ 查看 是否安裝成功 :npm

elec2.jpg

3、 搭建 vue 環境

1. 首先使用 vue-cli 搭建基礎 vue框架
複製代碼

elec3.png

和平時寫 vue項目同樣 選擇本身須要的

elec4.jpg

cd electron-demo 進入項目目錄

yarn serve 啓動項目

4、vue項目中添加 electron 模塊

輸入 vue add electron-builder 後 按下回車

elec5.jpg

安裝過程當中 此處咱們選擇 ^13.0.0 按下回車

elec6.jpg

因 electron 安裝時間過長 致使安裝失敗

elec7.jpg

elec8.jpg

解決辦法 直接使用 cnpm install 進行安裝

elec9.jpg

安裝 完成後 咱們使用 npm run electron:serve 運行項目 依舊是報錯的。請耐心往下看完

elec10.jpg

此處 咱們須要打開項目的目錄 找到 node_modules 下的 electron 。 刪除 這個文件之後 從新使用 cnpm install

elec12.jpg

至此。 electron 應用 已經搭建完成。 咱們使用 npm run electron:serve 進行啓動項目 ( windows 與 imac 同樣 )

elec13.jpg

已經完美運行成功 能夠看到桌面已經彈出 electron-demo 應用

5、查看electron+vue 項目目錄

相比於 基礎 vue 項目 咱們能夠發如今目錄中 多出一個 background.js 文件

elec14.jpg

此處能夠 配置 electron 應用的一些設置。 如窗口大小 、 是否能夠縮放、是否能夠移動窗口等等

elec15.jpg

Background.js 文件

'use strict'

import { app, protocol, BrowserWindow } from 'electron'
import { createProtocol } from 'vue-cli-plugin-electron-builder/lib'
import installExtension, { VUEJS_DEVTOOLS } from 'electron-devtools-installer'
const isDevelopment = process.env.NODE_ENV !== 'production'

// Scheme must be registered before the app is ready
protocol.registerSchemesAsPrivileged([
  { scheme: 'app', privileges: { secure: true, standard: true } }
])

async function createWindow() {
  // Create the browser window.
  const win = new BrowserWindow({
    width: 1120,
    height: 1090,
    minHeight: 700,
    minWidth: 1000,
    webPreferences: {

      // Use pluginOptions.nodeIntegration, leave this alone
      // See nklayman.github.io/vue-cli-plugin-electron-builder/guide/security.html#node-integration for more info
      nodeIntegration: process.env.ELECTRON_NODE_INTEGRATION,
      contextIsolation: !process.env.ELECTRON_NODE_INTEGRATION
    }
  })

  if (process.env.WEBPACK_DEV_SERVER_URL) {
    // Load the url of the dev server if in development mode
    await win.loadURL(process.env.WEBPACK_DEV_SERVER_URL)
    // if (!process.env.IS_TEST) win.webContents.openDevTools()
  } else {
    createProtocol('app')
    // Load the index.html when not in development
    win.loadURL('app://./index.html')
  }
}

// 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 (BrowserWindow.getAllWindows().length === 0) createWindow()
})

// 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', async () => {
  if (isDevelopment && !process.env.IS_TEST) {
    // Install Vue Devtools
    try {
      await installExtension(VUEJS_DEVTOOLS)
    } catch (e) {
      console.error('Vue Devtools failed to install:', e.toString())
    }
  }
  createWindow()
})

// Exit cleanly on request from parent process in development mode.
if (isDevelopment) {
  if (process.platform === 'win32') {
    process.on('message', (data) => {
      if (data === 'graceful-exit') {
        app.quit()
      }
    })
  } else {
    process.on('SIGTERM', () => {
      app.quit()
    })
  }
}

複製代碼

注意:打開和 關閉 開發者工具 請點擊。 操做欄 > view > Toggle Developer tools

咱們能夠在官網的文檔裏搜索一下。進行一些配置 ( 詳細配置請查看官網配置 ) 連接已經放在文章開頭啦

6、打包 electron 應用

打包應用 使用 npm run electron:build 進行打包 ( imac 系統 和 windows系統 同樣使用這個 )

此處 以 iMac 爲例 ( 下面會有 windows 解決方案 )

elec16.jpg

當咱們看到。 Build complete ! 時 就說明 打包成功 能夠查看 dist_electron / mac 裏面就是打包好的應用 ( 蘋果系統 )

windows 解決方案
  • 打包 windows 應用時 咱們須要將 整個項目的源碼 上傳到 windows 電腦上
  • 上傳到 windows 電腦上後 一樣須要安裝 cnpm 否則 electron 是安裝不上的
  • 打開項目目錄 刪除掉 node_modules 從新 cnpm install
  • 若是仍是沒法打包 刪除掉 node_modules / electron 目錄 從新 cnpm install
  • 而後運行 npm run electron:build 進行打包



PS:你們看了後以爲對本身有幫助能夠三連留言,歡迎提出寶貴意見,也歡迎各位對相關技術有興趣的開發者(由團隊開發人員微信號x118422邀請)入羣,在線解答討論數據可視化、優化圖表性能等方面的技術問題~

相關文章
相關標籤/搜索