electron+vue3+vite+antdesign

首先在我的项目vue3+vite+antdesign 基础上使用electron,两者并不冲突,只需要在我原先项目基础上安装electron就行,需要安装一些包以及配置就可以了,本次记录一下怎么使用,以及当时遇到的问题

前提是vue3+vite+antdesign 没问题哈!

1.安装包

cnpm i -D concurrently cross-env electron electron-builder electron-packager wait-on

2.配置package.json

注意electron时,"type": "commonjs",用vite项目的时候是"type": "module",(当时这个地方没注意,报错了)

  "type": "commonjs",

"scripts": {

    "serve": "vite",

    "build": "vite build",

    "preview": "vite preview",

    "start": "electron .",

    "packager": "electron-packager ./dist/ --platform=win32 --arch=x64 --overwrite",

    "electron": "wait-on tcp:3000 && cross-env IS_DEV=true electron .",

    "electron:dev": "concurrently -k \"cross-env BROWSER=none npm run dev\" \"npm run electron\"",

    "electron:build.win": "npm run build && electron-builder --win --dir",

    "electron:build.linux": "npm run build && electron-builder --linux appImage",

    "electron:build.test": "npm run build && electron-builder --dir",

    "electron:build.exe": "npm run build && electron-builder --win"

  },

添加:

 "main": "electron/electron.js",

 "build": {

    "appId": "com.my-website.my-app",

    "productName": "MyApp",

    "copyright": "Copyright © 2022 ${author}",

    "mac": {

      "category": "public.app-category.utilities"

    },

    "nsis": {

      "oneClick": false,

      "allowToChangeInstallationDirectory": true

    },

    "files": [

      "dist/**/*",

      "electron/**/*"

    ],

    "directories": {

      "buildResources": "assets",

      "output": "dist_electron"

    },

    "electronDownload": {

      "mirror": "https://npm.taobao.org/mirrors/electron/"

    }

  }

 3,vite.config.js

修改base

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
export default defineConfig({

 // base: env.VITE_BASE_URL,
  base: process.env.ELECTRON=="true" ? './' : "",
  plugins: [vue()]
})

4.入口文件

在项目根目录新建electron文件夹,再里面再新建electron.js

// electron/electron.js
const path = require('path');
const { app, BrowserWindow } = require('electron');

const isDev = process.env.IS_DEV == "true" ? true : false;

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

  // and load the index.html of the app.
  // win.loadFile("index.html");
  mainWindow.loadURL(
    isDev
      ? 'http://localhost:3000'
      : `file://${path.join(__dirname, '../dist/index.html')}`
  );
  // Open the DevTools.
  if (isDev) {
    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', () => {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

瑞瑞小同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值