干巴爹兔的博客 干巴爹兔的博客
首页
  • 前端文章

    • JavaScript
    • HTML
    • Vue
  • 学习笔记

    • JavaScript教程
    • React学习笔记
    • Electron学习笔记
  • 开源项目

    • cloud-app-admin
    • 下班了吗Vscode插件
    • Subversion变更单插件
  • Server

    • Django
  • 学习笔记

    • MySQL学习笔记
  • 运维

    • 服务器部署
    • Linux
  • 日常学习

    • 学习方法
关于
收藏
友链
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

干巴爹兔

卑微的前端打工人
首页
  • 前端文章

    • JavaScript
    • HTML
    • Vue
  • 学习笔记

    • JavaScript教程
    • React学习笔记
    • Electron学习笔记
  • 开源项目

    • cloud-app-admin
    • 下班了吗Vscode插件
    • Subversion变更单插件
  • Server

    • Django
  • 学习笔记

    • MySQL学习笔记
  • 运维

    • 服务器部署
    • Linux
  • 日常学习

    • 学习方法
关于
收藏
友链
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • Electron基础

  • Electron踩坑

    • 使用Vite+Ts+Vue3开发Electron的几个坑
    • 《Electron学习笔记》
    • Electron踩坑
    干巴爹兔
    2022-09-30
    目录

    使用Vite+Ts+Vue3开发Electron的几个坑

    # 一、开发环境与打包后的不一致问题

    在开发环境跑的好好的,但是打包完会出现找不到模块的问题,我这里使用的pnpm作为包管理器,electron似乎和它不是很适配,所以需要新建一个.npmrc文件,在里面加上一行

    shamefully-hoist=true
    
    1

    # 二、Mac环境执行脚本运行路径问题

    我在开发Electron应用的过程中需要使用node执行shell脚本,但是在打包后执行环境会出现问题,提示找不到path环境变量,这个时候就需要一个包fix-path,它能够正确的矫正执行环境错误的问题,值得一提的是,它的4.0版本抛弃了CommonJS的导入导出方式,如果需要在渲染进程使用require语法导入的话,需要降级到3.0.0版本使用

    # 三、与vue-router的兼容性问题

    在项目后期,我们使用了在渲染进程中引入window.require的方式剥离部分写在主进程的逻辑,但是在打包后控制台报错,找不到index.7873.js,经过排查发现,vue-router配置路由不能使用动态导入的方式,例如

    import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
    
    const routes = [
        {
            path: "/",
            name: "HomePage",
            redirect: { name: "Dashboard" },
            component: () => import("@/layouts/common-page.vue"),
            children: [
                {
                    path: "dashboard",
                    name: "Dashboard",
                    component: () => import("@/views/HomePage.vue")
                },
                {
                    path: "setting",
                    name: "Setting",
                    component: () => import("@/views/Setting.vue")
                },
                {
                    path: "about",
                    name: "About",
                    component: () => import("@/views/About.vue")
                }
            ]
        },
    ] as RouteRecordRaw[];
    
    const router = createRouter({
        history: createWebHashHistory(),
        routes: [...routes]
    });
    
    export default router
    
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35

    需要改造成

    import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
    import CommonPage from "@/layouts/common-page.vue";
    import Dashboard from "@/views/HomePage.vue";
    import Setting from "@/views/Setting.vue";
    import About from "@/views/About.vue";
    
    const routes = [
        {
            path: "/",
            name: "HomePage",
            redirect: { name: "Dashboard" },
            component: CommonPage,
            children: [
                {
                    path: "dashboard",
                    name: "Dashboard",
                    component: Dashboard
                },
                {
                    path: "setting",
                    name: "Setting",
                    component: Setting
                },
                {
                    path: "about",
                    name: "About",
                    component: About
                }
            ]
        },
    ] as RouteRecordRaw[];
    
    const router = createRouter({
        history: createWebHashHistory(),
        routes: [...routes]
    });
    
    export default router
    
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    编辑 (opens new window)
    上次更新: 2022/09/30, 15:12:49
    进程间的通讯

    ← 进程间的通讯

    最近更新
    01
    使用Vscode开发一个小插件
    10-21
    02
    Vscode插件配置项监听
    10-18
    03
    使用has属性构造必填效果
    10-14
    更多文章>
    Theme by Vdoing | Copyright © 2020-2023 互联网ICP备案: 闽ICP备18027236号
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式