当前位置: 首页 > news >正文

vue3+vite项目引入electron运行为桌面项目

一、安装electron

npm install --save-dev electron

二、项目根目录添加electron文件

在此文件夹中添加两个js文件:main.js、preload.js

main.js:

// Modules to control application life and create native browser window
const { app, BrowserWindow } = require('electron')
const path = require('node:path')

function createWindow () {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js')
    }
  })

  // and load the index.html of the app.
  mainWindow.loadFile('index.html')

  // Open the DevTools.
  // mainWindow.webContents.openDevTools()
}

// 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.whenReady().then(() => {
  createWindow()

  app.on('activate', function () {
    // 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()
  })
})

// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', function () {
  if (process.platform !== 'darwin') app.quit()
})

// 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.

preload.js:

/**
 * The preload script runs before `index.html` is loaded
 * in the renderer. It has access to web APIs as well as
 * Electron's renderer process modules and some polyfilled
 * Node.js functions.
 *
 * https://www.electronjs.org/docs/latest/tutorial/sandbox
 */
window.addEventListener('DOMContentLoaded', () => {
  const replaceText = (selector, text) => {
    const element = document.getElementById(selector)
    if (element) element.innerText = text
  }

  for (const type of ['chrome', 'node', 'electron']) {
    replaceText(`${type}-version`, process.versions[type])
  }
})

三、配置package.json文件

删除:“type”: “module”
添加:“main”: “electron/main.js”,
添加:“start”: “electron .”

示例:

{
  "name": "electron_vue3",
  "private": true,
  "version": "0.0.0",
  "main": "electron/main.js",
  "scripts": {
    "dev": "vite",
    "build": "vue-tsc -b && vite build",
    "preview": "vite preview",
    "start": "electron ."
  },
  "dependencies": {
    "vue": "^3.5.13"
  },
  "devDependencies": {
    "@types/node": "^22.10.10",
    "@vitejs/plugin-vue": "^5.2.1",
    "@vue/tsconfig": "^0.7.0",
    "electron": "^34.0.1",
    "typescript": "~5.6.2",
    "vite": "^6.0.5",
    "vue-tsc": "^2.2.0"
  }
}

四、运行

// 运行为桌面项目
npm run start

// 运行为网页
npm run dev

相关文章:

  • fork: retry: No child processes-linux18
  • Kotlin 2.1.0 入门教程(十七)接口
  • SpringCloud面试题----微服务下为什需要链路追踪系统
  • 2月14(信息差)
  • 【算法工程】解决linux下Aspose.slides提示No usable version of libssl found以及强化推理模型的短板
  • Win7本地化部署deepseek-r1等大模型详解
  • Base64 PDF解析器
  • 大模型为什么离不开PyTorch
  • python使用try-except-else处理异常
  • AndroidStudio查看Sqlite和SharedPreference
  • 利用蓝耘智算平台深度搭建deepseek R1模型,进行深度机器学习
  • git 提示 fatal: The remote end hung up unexpectedly
  • 网络安全 “免疫力”:从人体免疫系统看防御策略
  • Windchill开发-电子仓相关对象信息查询SQL
  • CCF-GESP 等级考试 2024年9月认证C++二级真题解析
  • 《网络编程卷2:进程间通信》第八章:共享内存深度解析与多进程高性能通信实践
  • 【前端OCR】如何用paddlejs开发一个属于前端本地的OCR文本识别功能
  • 江科大51单片机学习笔记(2)
  • 在Linux中Redis不支持lua脚本的处理方法
  • 基于 GEE 计算研究区年均地表温度数据
  • 做网站给源码吗/如何提升网站搜索排名
  • 图片 网站开发/百度推广代理赚钱
  • 云端物联网管理平台/宁波企业seo推广
  • 简述网站建设的五类成员/合肥瑶海区房价
  • 2016企业网站建设合同/百度竞价代运营外包
  • 肇庆网站制作/杭州seo教程