Electron结合Vue使用说明
创始人
2024-03-23 18:02:46
0

Electron结合Vue使用说明

1. 创建Vue项目:

npm init vue

2. 创建Electron项目

npm init electron-app@latest electron-project --template=webpack-typescript

npm install copy-webpack-plugin --save-dev

3. 项目配置

1.配置Vue项目:

1)配置base路径

// vite.config.ts
export default defineConfig({plugins: [vue(), vueJsx()],resolve: {alias: {"@": fileURLToPath(new URL("./src", import.meta.url)),},},base: "./", // 新增配置(解决绝对路径导致HTML请求文件失败的问题)
});

2)配置路由为哈希路由

// router/index.ts
import { createRouter, createWebHashHistory } from "vue-router";
import HomeView from "../views/HomeView.vue";const router = createRouter({history: createWebHashHistory(import.meta.env.BASE_URL), // 修改为Hash模式routes: [{path: "/",name: "home",component: HomeView,},{path: "/about",name: "about",component: () => import("../views/AboutView.vue"),},],
});export default router;
2.配置Electron项目

1)修改入口文件,将开发环境与Vue结合

// src/index.ts
// 用于Vue开发环境
if(process.env.npm_lifecycle_event === 'start'){mainWindow.loadURL(<前端项目地址,如('http://localhost:5174')>);
} else {mainWindow.loadURL(MAIN_WINDOW_WEBPACK_ENTRY);
}

2)修改webpack配置,将Vue打包文件整合至Electron打包环境

// webpack.renderer.config.ts
export const rendererConfig: Configuration = {module: {rules},plugins: [// 新增,在打包是将Vue项目整合至Electronnew CopyPlugin({patterns: [{// 将dist目录文件(排除index.html)拷贝至.webpack/renderer/main_windowfrom: "./dist/**",to: ({ context, absoluteFilename }) => {const relativePath = path.relative(context, absoluteFilename).replace(/^dist/, ".");const basePath = path.resolve(__dirname,".webpack/renderer/main_window");return path.resolve(basePath, relativePath);},globOptions: {ignore: ["**/dist/index.html"],},},],}),...plugins,],resolve: {extensions: [".js", ".ts", ".jsx", ".tsx", ".css"],},
};

3)修改forge入口

const config: ForgeConfig = {
packagerConfig: {},rebuildConfig: {},makers: [new MakerSquirrel({}),new MakerZIP({}, ["darwin"]),new MakerRpm({}),new MakerDeb({}),],plugins: [new WebpackPlugin({mainConfig,renderer: {config: rendererConfig,entryPoints: [{html: "./dist/index.html", // 将HTML文件入口指向Vue打包文件js: "./src/renderer.ts",name: "main_window",preload: {js: "./src/preload.ts",},},],},}),],
};

4. 开发

1.运行Vue项目
2.运行Electron项目

5. 打包

1.将Vue打包后的 dist 目录复制到Electron工程根目录
2.打包Electron

相关内容

热门资讯

中证A500ETF摩根(560... 8月22日,截止午间收盘,中证A500ETF摩根(560530)涨1.19%,报1.106元,成交额...
A500ETF易方达(1593... 8月22日,截止午间收盘,A500ETF易方达(159361)涨1.28%,报1.104元,成交额1...
何小鹏斥资约2.5亿港元增持小... 每经记者|孙磊    每经编辑|裴健如 8月21日晚间,小鹏汽车发布公告称,公司联...
中证500ETF基金(1593... 8月22日,截止午间收盘,中证500ETF基金(159337)涨0.94%,报1.509元,成交额2...
中证A500ETF华安(159... 8月22日,截止午间收盘,中证A500ETF华安(159359)涨1.15%,报1.139元,成交额...
科创AIETF(588790)... 8月22日,截止午间收盘,科创AIETF(588790)涨4.83%,报0.760元,成交额6.98...
创业板50ETF嘉实(1593... 8月22日,截止午间收盘,创业板50ETF嘉实(159373)涨2.61%,报1.296元,成交额1...
港股异动丨航空股大幅走低 中国... 港股航空股大幅下跌,其中,中国国航跌近7%表现最弱,中国东方航空跌近5%,中国南方航空跌超3%,美兰...
电网设备ETF(159326)... 8月22日,截止午间收盘,电网设备ETF(159326)跌0.25%,报1.198元,成交额409....
红利ETF国企(530880)... 8月22日,截止午间收盘,红利ETF国企(530880)跌0.67%,报1.034元,成交额29.0...