最新簡約實效vite+electron+vue實戰(zhàn)

特點:干預(yù)最少,代碼最少,依賴最新

主要是在window下進(jìn)行,linux參照執(zhí)行

搭建環(huán)境

安裝

  1. 安裝nodejs
  2. 安裝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)

  1. 思路 先搭建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搭建好了


瀏覽器里看到
  1. 思路 加入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,暫時空白。

  1. 思路開發(fā)模式下,先vite生成,在通過electron
    具體代碼就是main.js中的
win.loadURL('localhost:3000')

啟動vite

vite

新開一個命令行,啟動electron

electron .
electron顯示效果
  1. 思路 按照electron最新的思路,vue頁面中不能使用nodejs和electron,electron手冊中要求通過preload.js隔離vue頁面和main.js的交互。

這是electron手冊中的原文

This feature is incredibly useful for two main purposes:

  • By exposing ipcRenderer helpers 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 window global 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頁面。

  1. 存在路徑問題:
    vite默認(rèn)生成的url為絕對路徑,用electron包裹時,會出現(xiàn)路徑問題,解決辦法為,在vite配置文件中加入base設(shè)置。
export default defineConfig({
  //...
  base: './', // 這里決定了生成的html采用相對路徑
  //...
})

整理,發(fā)到gitee上,供大家下載。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容