记录一次electron踩坑

雨点打透心脏的1/2处 2023-01-08 03:29 461阅读 0赞

自己弄的框架中存在各种问题 搭建自己用electron的时候也许会碰到
Vue2.x版本
https://github.com/dmhsq/electron-vue-dmhsq
或者
https://github.com/dmhsq/electron-vue/tree/main/template
Vue3.x版本
https://github.com/dmhsq/electron-vue3-dmhsq
等教程写完 教程地址 会抽空封装几个工具

解决如下

踩坑

  • 1.解决fs.existsSync is not a function
  • 2.解决electron发送请求时http://变file:// (也能解决代理)
  • 3.实现登陆以及切换用户
    • 方案一 主进程使用菜单切换
    • 方案二 Vue开发的页面触发切换

自己摸索的 可能有问题 大佬勿喷

1.解决fs.existsSync is not a function

vue引入electron的ipcRenderer等其它模块会出现这种情况
解决方案
自定义事件 使用渲染进程捕捉

在你的页面代码(比如vue文件)中加入

  1. let cus = new CustomEvent("test_event",{ detail:{ 你的数据}});
  2. window.dispatchEvent(cus);

解释:
cus定义了一个window事件 detail传递数据
test_event:自定义事件名
window.dispatchEvent(cus);触发自定义事件

在渲染进程
在这里插入图片描述
添加以下代码负责监听

  1. const { ipcRenderer } = require("electron");
  2. window.addEventListener('test_event',(e)=>{
  3. console.log('开始了哦')
  4. console.log(e.detail)//这里就是你发送的数据
  5. ipcRenderer.send("test",e.detail);
  6. })

渲染进程使用ipcRenderer向主进程发送数据
说明test为向主进程发送的事件名

以下为主进程接收代码

  1. ipcMain.on("test", () => {
  2. ...做处理
  3. });

2.解决electron发送请求时http://变file:// (也能解决代理)

发送请求时触发一个事件
比如

  1. export const find = params => {
  2. let cus = new CustomEvent("axioes");
  3. window.dispatchEvent(cus);
  4. return axios.get("/api/find", { params: params });
  5. };

在渲染进程中

  1. window.addEventListener('axioes',()=>{
  2. ipcRenderer.send('axioso')
  3. })

在主进程中(需要引入session模块)

  1. const {
  2. app,
  3. BrowserWindow,
  4. Notification,
  5. Menu,
  6. MenuItem,
  7. ipcMain,
  8. net,
  9. session
  10. } = require("electron");
  11. ipcMain.addListener("axioso", () => {
  12. resq();
  13. });
  14. function resq() {
  15. const filter = {
  16. urls: ["*://api/*", "file://api/*"]
  17. };
  18. session.defaultSession.webRequest.onBeforeRequest(
  19. filter,
  20. (details, callback) => {
  21. console.log(122131231);
  22. console.log(details);
  23. // details.url = "http://www.baidu.com"
  24. let srt = details.url;
  25. details.url = "http://localhost:8080" + srt.substr(10);
  26. callback({ redirectURL: details.url });
  27. console.log(details);
  28. }
  29. );
  30. }

说明:

  1. const filter = {
  2. urls: ["*://api/*", "file://api/*"]
  3. };

定义匹配api字符串的请求的拦截规则

说明:

  1. session.defaultSession.webRequest.onBeforeRequest(
  2. filter,
  3. (details, callback) => {
  4. console.log(122131231);
  5. console.log(details);
  6. // details.url = "http://www.baidu.com"
  7. let srt = details.url;
  8. details.url = "http://localhost:8080" + srt.substr(10);
  9. callback({ redirectURL: details.url });
  10. console.log(details);
  11. }
  12. );

在发送请求之前 改写请求地址
原本我的请求地址是http://localhost:8086/edusys/admin/find
使用代理为 /api/find
我的vue项目端口为8080
所以需要将”/api/find”变为”http://localhost:8080/api/find“
所以我的nginx配置如下

  1. server {
  2. listen 8080;
  3. server_name localhost;
  4. location / {
  5. root html;
  6. index index.html index.htm;
  7. }
  8. location /api {
  9. proxy_pass http://localhost:8086/edusys/admin;
  10. }
  11. }

3.实现登陆以及切换用户

两种方案

方案一 主进程使用菜单切换

在这里插入图片描述主进程监听菜单事件
在这里插入图片描述
主进程处理代码

  1. function changeUser() {
  2. const nm = new Menu();//创建空菜单
  3. Menu.setApplicationMenu(nm);//菜单设置为空
  4. createWindows();//创建登陆窗口
  5. wins.close();//关闭原先的大窗口这里的win是createWindowMain()创建窗口时返回的win
  6. }
  7. function createWindowMain() {
  8. const win = new BrowserWindow({
  9. width: 1500,
  10. height: 1000,
  11. webPreferences: {
  12. nodeIntegration: true
  13. }
  14. });
  15. win.loadFile(__dirname + "/dist/index.html");
  16. // win.webContents.openDevTools();
  17. reloadTheWin(win);
  18. return win;
  19. }
  20. function createWindows() {
  21. const win = new BrowserWindow({
  22. width: 400,
  23. height: 600,
  24. webPreferences: {
  25. nodeIntegration: true
  26. }
  27. });
  28. win.loadFile(__dirname + "./login.html");
  29. // reloadTheWins(win);
  30. }

方案二 Vue开发的页面触发切换

在这里插入图片描述

这是切换按钮所在的菜单的代码(头部导航的部分代码)
页面点击切换用户触发自定义事件

  1. <el-dropdown>
  2. <span style="color: black">
  3. 教务处<i class="el-icon-arrow-down el-icon--right"></i>
  4. </span>
  5. <el-dropdown-menu slot="dropdown">
  6. <el-dropdown-item>退出</el-dropdown-item>
  7. <el-dropdown-item @click.native="changeUser">切换用户</el-dropdown-item>
  8. </el-dropdown-menu>
  9. </el-dropdown>
  10. <script> export default { name: "MyHeader", methods:{ changeUser(){ console.log("改变用户") let cuss = new CustomEvent("changeUsers");//自定义事件 window.dispatchEvent(cuss);//触发自定义事件 } } }; </script>

渲染进程处理代码

  1. window.addEventListener("changeUsers", () => { //监听changeUsers事件
  2. ipcRenderer.send("changeUsr");//向主进程发送通知
  3. })

主进程处理代码

  1. ipcMain.on('changeUsr',()=>{
  2. changeUser();
  3. });
  4. function changeUser() {
  5. const nm = new Menu();//创建空菜单
  6. Menu.setApplicationMenu(nm);//菜单设置为空
  7. createWindows();//创建登陆窗口
  8. wins.close();//关闭原先的大窗口这里的win是createWindowMain()创建窗口时返回的win
  9. }
  10. function createWindowMain() {
  11. const win = new BrowserWindow({
  12. width: 1500,
  13. height: 1000,
  14. webPreferences: {
  15. nodeIntegration: true
  16. }
  17. });
  18. win.loadFile(__dirname + "/dist/index.html");
  19. // win.webContents.openDevTools();
  20. reloadTheWin(win);
  21. return win;
  22. }
  23. function createWindows() {
  24. const win = new BrowserWindow({
  25. width: 400,
  26. height: 600,
  27. webPreferences: {
  28. nodeIntegration: true
  29. }
  30. });
  31. win.loadFile(__dirname + "./login.html");
  32. // reloadTheWins(win);
  33. }

  大家好,我是代码哈士奇,是一名软件学院网络工程的学生,因为我是“狗”,狗走千里吃肉。想把大学期间学的东西和大家分享,和大家一起进步。但由于水平有限,博客中难免会有一些错误出现,有纰漏之处恳请各位大佬不吝赐教!暂时只在csdn这一个平台进行更新,博客主页:https://blog.csdn.net/qq_42027681。

未经本人允许,禁止转载

在这里插入图片描述

后续会推出

前端:vue入门 vue开发小程序 等
后端: java入门 springboot入门等
服务器:mysql入门 服务器简单指令 云服务器运行项目
python:推荐不温卜火 一定要看哦
一些插件的使用等

大学之道亦在自身,努力学习,热血青春
如果对编程感兴趣可以加入我们的qq群一起交流:974178910
在这里插入图片描述

有问题可以下方留言,看到了会回复哦

发表评论

表情:
评论列表 (有 0 条评论,461人围观)

还没有评论,来说两句吧...

相关阅读

    相关 electron安装

    记录一下前段时间使用electron踩的坑 1、一定要使用cnpm安装 cnpm是淘宝搞的npm安装工具,为什么不推荐npm安装,因为安装速度慢而且容易报错!!! 贴

    相关 electron-updater自动更新

    由于项目的更新服务很不稳定,所以想着换一个更新服务。百度一下,立马就看到了electron-updater。网上有很多关于如何使用的demo,我就跟着试了一下,发现了很多的问题

    相关 grafana记录

    1.鼠标移动悬停时数据不变更 鼠标悬停时,数据时间一直不变更,这样鼠标移动时,就没法一眼看出正确数据。比如图中,鼠标悬停在8-29了,但是数据详情还是8-27. ![