特點:干預(yù)最少,代碼最少,依賴最新
主要是在window下進(jìn)行,linux參照執(zhí)行
搭建環(huán)境
安裝
- 安裝nodejs
- 安裝git
配置
明確全局包位置,自定義的,比如 D:\Server\cache\javascript 我們用 GLOBAL_PATH 標(biāo)記。
設(shè)置NPM
npm config edit
# 在彈出的ini文件中修改:
cache=GLOBAL_PATH
ELECTRON_BUILDER_BINARIES_MIRROR=http://npm.taobao.org/mirrors/electron-builder-binaries/
electron_mirror=https://cdn.npm.taobao.org/dist/electron/
registry=https://registry.npm.taobao.org/
安裝yarn
npm i -g yarn
yarn config set cache-folder GLOBAL_PATH
安裝vite
// npm i -g vite
yarn global add vite
開始實戰(zhàn)
-
思路 先搭建
vite+vue
定位到一個工作目錄,比如D:\www,先按照vite的手冊創(chuàng)建項目
yarn create @vitejs/app electron-vite-vue --template vue
cd electron-vite-vue
yarn
yarn dev

命令行顯示
這時候,在瀏覽器里可以看到頁面。vite+vue搭建好了

瀏覽器里看到
-
思路 加入
electron,按照electron的手冊執(zhí)行
yarn add --dev electron
在項目根目錄下的package.json中加入
{
"name": "electron-vite-vue",
"version": "1.0.0",
"main": "main.js",
"license": "MIT",
"scripts": {
"start": "electron ."
}
}
整個文件大致是這樣的
{
"name": "electron-vite-vue",
"version": "0.0.1",
"main": "main.js",
"license": "MIT",
"scripts": {
"dev": "vite",
"build": "vite build",
"serve": "vite preview",
"start": "electron ."
},
"dependencies": {
"vue": "^3.0.5"
},
"devDependencies": {
"electron": "^13.1.6",
"@vitejs/plugin-vue": "^1.2.4",
"@vue/compiler-sfc": "^3.0.5",
"vite": "^2.4.0"
}
}
在項目根目錄下新建main.js
const { app, BrowserWindow } = require('electron')
const path = require('path')
function createWindow () {
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})
win.loadURL('localhost:3000')
}
app.whenReady().then(() => {
createWindow()
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
}
)})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
在項目根目錄下新建preload.js,暫時空白。
-
思路開發(fā)模式下,先vite生成,在通過electron
具體代碼就是main.js中的
win.loadURL('localhost:3000')
啟動vite
vite
新開一個命令行,啟動electron
electron .

electron顯示效果
-
思路 按照electron最新的思路,vue頁面中不能使用nodejs和electron,electron手冊中要求通過
preload.js隔離vue頁面和main.js的交互。
這是electron手冊中的原文
This feature is incredibly useful for two main purposes:
- By exposing
ipcRendererhelpers to the renderer, you can use inter-process communication (IPC) to trigger main process tasks from the renderer (and vice-versa).- If you're developing an Electron wrapper for an existing web app hosted on a remote URL, you can add custom properties onto the renderer's
windowglobal that can be used for desktop-only logic on the web client's side.
測試一個,在preload.js中添加
const { contextBridge } = require('electron')
// 在vue頁面中引用window.$electron.titile,獲得 'electron-vite-vue'
contextBridge.exposeInMainWorld('$electron', {
title: 'electron-vite-vue'
})
修改src/app.vue
<template>
<img alt="Vue logo" src="./assets/logo.png" />
<!--修改--><HelloWorld :msg="message" />
</template>
<script setup>
import HelloWorld from './components/HelloWorld.vue'
// 引用preload.js中定義的常量
const message = window.$electron.title
// This starter template is using Vue 3 experimental <script setup> SFCs
// Check out https://github.com/vuejs/rfcs/blob/script-setup-2/active-rfcs/0000-script-setup.md
</script>
刷新electron頁面。
- 存在路徑問題:
vite默認(rèn)生成的url為絕對路徑,用electron包裹時,會出現(xiàn)路徑問題,解決辦法為,在vite配置文件中加入base設(shè)置。
export default defineConfig({
//...
base: './', // 這里決定了生成的html采用相對路徑
//...
})
整理,發(fā)到gitee上,供大家下載。