• electron+vite+vue3项目搭建


    1.创建项目

    npm init vite@latest或者npm create vite)

    2.安装electron相关依赖

    1. npm install electron -D
    2. npm install vite-plugin-electron -D
    3. //常用版本: "vite-plugin-electron": "^0.8.3"; "electron": "^19.0.10",

    3.在 vite.config.ts 中,配置 Electron 入口文件

    1. import { defineConfig } from 'vite'
    2. import vue from '@vitejs/plugin-vue'
    3. import electron from 'vite-plugin-electron'
    4. import path, { resolve, join } from 'path'
    5. export default defineConfig({
    6. plugins: [vue(), electron({
    7. main: {
    8. // 配置 Electron 入口文件
    9. entry: "electron/index.js"
    10. }
    11. })],
    12. resolve: {
    13. alias: {
    14. "@": resolve(__dirname, 'src'), // 这里是将src目录配置别名为 @ 方便在项目中导入src目录下的文件
    15. }
    16. },
    17. })

    4.编写 electron / index.js(electron主要配置)

    1. // app 控制应用程序的事件生命周期(相当于应用程序)
    2. // BrowserWindow 创建并控制浏览器窗口(相当于打开桌面弹框)
    3. // screen.getPrimaryDisplay()窗口对象
    4. // ipcMain当在主进程中使用它的时候,它控制着由渲染进程(web page)发送过来的异步或同步消息.从渲染进程发送过来的消息将触发事件.
    5. // Tray一个图标,这个图标处于正在运行的系统的通知区
    6. // Menu设置菜单
    7. import { app, BrowserWindow, screen, ipcMain, Tray, Menu } from 'electron'
    8. import path from 'path'
    9. process.env['ELECTRON_DISABLE_SECURITY_WARNINGS'] = 'true'
    10. // 定义全局变量,获取窗口实例
    11. const windows = {
    12. // 主窗口
    13. main: {
    14. win: null
    15. },
    16. // 登录窗口
    17. login: {
    18. win: null
    19. },
    20. exit: {
    21. win: null,
    22. width: 300,
    23. height: 200,
    24. },
    25. }
    26. const INITMENU = 0x116; //当弹出系统菜单时
    27. //禁用窗口自带的右键菜单
    28. function disableContextMenu(win) {
    29. win.hookWindowMessage(INITMENU, () => {
    30. win.setEnabled(false);
    31. setTimeout(() => {
    32. win.setEnabled(true);
    33. }, 20);
    34. return true;
    35. })
    36. }
    37. // 创建主窗口
    38. const createWindow = () => {
    39. let {
    40. width,
    41. height
    42. } = screen.getPrimaryDisplay().workArea;
    43. windows.main.win = new BrowserWindow({
    44. width: width,
    45. height: height,
    46. show: true, //先不显示窗口,等窗口准备好了才显示,防止渲染未完成时出现白框
    47. frame: false,
    48. movable: true,
    49. resizable: true,
    50. webPreferences: {
    51. devTools: true,
    52. // 集成网页和 Node.js,也就是在渲染进程中,可以调用 Node.js 方法
    53. nodeIntegration: true,
    54. contextIsolation: false,
    55. webSecurity: false, //是否允许渲染进程访问本地文件
    56. //允许html页面上的javascipt代码访问nodejs 环境api代码的能力(与node集成的意思)
    57. }
    58. })
    59. // 开发环境 development
    60. // 是生产环境 production
    61. if (process.env.NODE_ENV != 'development') {
    62. console.log(1)
    63. windows.main.win.loadFile(path.join(__dirname, "../index.html"));
    64. // win.webContents.openDevTools();
    65. } else {
    66. console.log(2)
    67. windows.main.win.loadURL(`http://${process.env['VITE_DEV_SERVER_HOST']}:${process.env['VITE_DEV_SERVER_PORT']}` + "/index.html");
    68. if (!process.env.IS_TEST) windows.main.win.webContents.openDevTools();
    69. }
    70. disableContextMenu(windows.main.win);
    71. }
    72. //托盘区图标
    73. var appTray = null;
    74. function setTray() {
    75. //设置菜单内容
    76. let trayMenu = [{
    77. label: '退出', //菜单名称
    78. click: function () { //点击事件
    79. app.quit();
    80. }
    81. }];
    82. let trayIcon = null;
    83. //设置托盘区图标
    84. if (process.env.NODE_ENV != 'development') {
    85. trayIcon = path.join(__dirname, '../favicon.ico');
    86. } else {
    87. trayIcon = path.join(__dirname, '../../public/favicon.ico');
    88. }
    89. appTray = new Tray(trayIcon);
    90. //设置菜单
    91. const contextMenu = Menu.buildFromTemplate(trayMenu);
    92. //设置悬浮提示
    93. appTray.setToolTip('数据校验软件');
    94. //设置
    95. appTray.setContextMenu(contextMenu);
    96. //点击图标
    97. appTray.on('click', function () {
    98. //显示主程序
    99. if (windows.main.win) windows.main.win.show();
    100. });
    101. }
    102. // app.on('activate', () => {
    103. // console.log(333)
    104. // // On macOS it's common to re-create a window in the app when the
    105. // // dock icon is clicked and there are no other windows open.
    106. // if (BrowserWindow.getAllWindows().length === 0) createWindow()
    107. // })
    108. // 初始化app(在 Electron 完成初始化时触发)
    109. app.whenReady().then(() => {
    110. createWindow();
    111. setTray();
    112. addIpc()
    113. });
    114. const addIpc = () => {
    115. // 最小化
    116. ipcMain.on('min-app', () => {
    117. windows.main.win.minimize();
    118. });
    119. // 退出程序
    120. ipcMain.on('recome', () => {
    121. windows.main.win.close();
    122. })
    123. }

    5.在 package.json 中,增加 main 字段,去掉 type 字段

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

    6.运行项目即可打开窗口

    npm run dev

  • 相关阅读:
    解决ios向mac复制文字不成功的一种方法
    既要技术制胜,也要体验为王:今天我们需要怎样的WLAN?
    nvm 安装 管理node版本 注意事项
    JavaScript系列之变量
    搞懂 Vue3 中的各种 ref:toRef,toRefs,isRef,unref...
    [附源码]Python计算机毕业设计Django的残障人士社交平台
    1.本地备份数据库(windows)- navicat 定时备份数据库
    IOS 快速获取加密方法,加密数据
    Python中学习调试requests模块时出现的大坑(1)
    视频上传阿里云,如何获取视频的某一帧作为封面??
  • 原文地址:https://blog.csdn.net/m0_63701303/article/details/134270999