Electron 官網地址 點此 : electron 官方地址javascript
Electron 至關於一個瀏覽器的外殼 , 咱們將 編寫的 HTML , CSS , Javascript 網頁程序 嵌入進 Electron 裏面html
以便於在桌面上進行運行。 通俗來說它就是一個軟件 , 如 QQ 、網易雲音樂、優酷視頻 等等。功能至強大vue
超乎你的想象java
安裝 Node.js 壞境node
Node.js 官網地址 點此 node.js 官方地址git
安裝 Vue Cligithub
npm install -g @vue/cli
複製代碼
安裝 淘寶鏡像 cnpm ( 因 electron 包 特別大 本人嘗試過 npm 或 yarn 的 方法 須要等待很長時間 )web
全局安裝 Electronvue-cli
cnpm install electron -g
//若是安裝仍是 特比慢 或 不想安裝cnpn 能夠聯繫咱們
複製代碼
查看 是否安裝成功 :npm
1. 首先使用 vue-cli 搭建基礎 vue框架
複製代碼
和平時寫 vue項目同樣 選擇本身須要的
cd electron-demo 進入項目目錄
yarn serve 啓動項目
輸入 vue add electron-builder 後 按下回車
安裝過程當中 此處咱們選擇 ^13.0.0 按下回車
因 electron 安裝時間過長 致使安裝失敗
解決辦法 直接使用 cnpm install 進行安裝
安裝 完成後 咱們使用 npm run electron:serve 運行項目 依舊是報錯的。請耐心往下看完
此處 咱們須要打開項目的目錄 找到 node_modules 下的 electron 。 刪除 這個文件之後 從新使用 cnpm install
至此。 electron 應用 已經搭建完成。 咱們使用 npm run electron:serve 進行啓動項目 ( windows 與 imac 同樣 )
已經完美運行成功 能夠看到桌面已經彈出 electron-demo 應用
相比於 基礎 vue 項目 咱們能夠發如今目錄中 多出一個 background.js 文件
此處能夠 配置 electron 應用的一些設置。 如窗口大小 、 是否能夠縮放、是否能夠移動窗口等等
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
咱們能夠在官網的文檔裏搜索一下。進行一些配置 ( 詳細配置請查看官網配置 ) 連接已經放在文章開頭啦
打包應用 使用 npm run electron:build 進行打包 ( imac 系統 和 windows系統 同樣使用這個 )
此處 以 iMac 爲例 ( 下面會有 windows 解決方案 )
當咱們看到。 Build complete ! 時 就說明 打包成功 能夠查看 dist_electron / mac 裏面就是打包好的應用 ( 蘋果系統 )