Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

vue electron

作者:颓废的爱情   发布日期:2025-03-25   浏览:64

// main.js - Electron 主进程文件
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'),
      nodeIntegration: true,
      contextIsolation: false
    }
  });

  // 加载 Vue 应用的 index.html 文件
  win.loadFile('dist/index.html');
}

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 - 预加载脚本
const { contextBridge } = require('electron');

contextBridge.exposeInMainWorld('versions', {
  node: () => process.versions.node,
  chrome: () => process.versions.chrome,
  electron: () => process.versions.electron
});

// src/main.js - Vue 前端入口文件
import { createApp } from 'vue';
import App from './App.vue';

createApp(App).mount('#app');

// public/index.html - Vue 应用的 HTML 文件
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Vue Electron App</title>
  </head>
  <body>
    <div id="app"></div>
  </body>
</html>

// package.json - 项目配置文件
{
  "name": "vue-electron-app",
  "version": "1.0.0",
  "main": "main.js",
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint",
    "electron:build": "vue-cli-service build && electron ."
  },
  "dependencies": {
    "vue": "^3.0.0"
  },
  "devDependencies": {
    "@vue/cli-plugin-babel": "~4.5.0",
    "@vue/cli-plugin-eslint": "~4.5.0",
    "@vue/cli-service": "~4.5.0",
    "electron": "^13.0.0",
    "eslint": "^7.32.0",
    "eslint-plugin-vue": "^7.0.0"
  }
}

解释说明

  1. main.js: 这是 Electron 的主进程文件,负责创建浏览器窗口并加载 Vue 应用的 index.html 文件。它还设置了 Web 引擎的偏好设置,如启用 Node.js 集成。

  2. preload.js: 这是一个预加载脚本,用于在渲染进程中暴露一些 API 给前端代码使用。这里我们通过 contextBridge 暴露了 Electron、Node 和 Chrome 的版本信息。

  3. src/main.js: 这是 Vue 应用的前端入口文件,负责创建 Vue 实例并挂载到 DOM 元素上。

  4. public/index.html: 这是 Vue 应用的 HTML 文件,包含了一个 div 元素作为 Vue 应用的挂载点。

  5. package.json: 这是项目的配置文件,包含了依赖项和脚本命令。electron:build 脚本用于构建 Vue 应用并启动 Electron 应用。

通过这些文件和配置,你可以创建一个基于 Vue 和 Electron 的桌面应用程序。

上一篇:vue3 validator

下一篇:vue项目打包

大家都在看

vue.config.js configu

node.js vue

vue查看版本

vue等待几秒

vue3 setup computed

vue screenfull

vue json.stringify

vue 遍历list

typescript vue

vue 复选框

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站