Electron-Vue 脚手架避坑实录,兼容Win11,升级electron22,清理控制台错误

去年的还是有用的,大家继续看,今年再补充一些
Electron-Vue 异常处理方案 M1 和 Window10_electron异常处理-CSDN博客

 代码gitee.com地址

electron-demo: electron 22 初始代码开发和讲解

升级electron为22版本(这个版本承上启下,比较好用,建议使用)package.json和package-lock.json

这里把devtron干掉了,因为无论怎么尝试,代码版本差异太大,无法适配,所以其他人就不必费心了,但是devtron源码很值得学习,搞一个内置进程看板很方便,有空我看看怎么重写

{
  "name": "electron-demo",
  "version": "0.0.1",
  "author": "wangsen <wangsen@zingfront.com>",
  "description": "An electron Demo",
  "license": null,
  "main": "./dist/electron/main.js",
  "scripts": {
    "build": "node .electron-vue/build.js && electron-builder",
    "build:dir": "node .electron-vue/build.js && electron-builder --dir",
    "build:clean": "cross-env BUILD_TARGET=clean node .electron-vue/build.js",
    "build:web": "cross-env BUILD_TARGET=web node .electron-vue/build.js",
    "dev": "node .electron-vue/dev-runner.js",
    "lint": "eslint --ext .js,.vue -f ./node_modules/eslint-friendly-formatter src",
    "lint:fix": "eslint --ext .js,.vue -f ./node_modules/eslint-friendly-formatter --fix src",
    "pack": "npm run pack:main && npm run pack:renderer",
    "pack:main": "cross-env NODE_ENV=production webpack --progress --colors --config .electron-vue/webpack.main.config.js",
    "pack:renderer": "cross-env NODE_ENV=production webpack --progress --colors --config .electron-vue/webpack.renderer.config.js",
    "postinstall": "npm run lint:fix"
  },
  "build": {
    "productName": "electron-demo",
    "appId": "e-demo",
    "directories": {
      "output": "build"
    },
    "files": [
      "dist/electron/**/*"
    ],
    "dmg": {
      "contents": [
        {
          "x": 410,
          "y": 150,
          "type": "link",
          "path": "/Applications"
        },
        {
          "x": 130,
          "y": 150,
          "type": "file"
        }
      ]
    },
    "mac": {
      "icon": "build/icons/icon.icns"
    },
    "win": {
      "icon": "build/icons/icon.ico"
    },
    "linux": {
      "icon": "build/icons"
    }
  },
  "dependencies": {
    "@electron/remote": "2.0.1",
    "axios": "1.6.8",
    "vue": "^2.6.11",
    "vue-electron": "^1.0.6",
    "vue-router": "^3.0.1",
    "vuex": "^3.0.1",
    "vuex-electron": "1.0.2"
  },
  "devDependencies": {
    "@types/classnames": "^2.2.6",
    "@types/color": "3.0.0",
    "@types/color-convert": "^1.9.0",
    "@types/lodash": "4.14.144",
    "@types/node": "12.12.54",
    "ajv": "^6.5.0",
    "babel-core": "^6.26.3",
    "babel-eslint": "^8.2.3",
    "babel-loader": "^7.1.4",
    "babel-minify-webpack-plugin": "^0.3.1",
    "babel-plugin-transform-runtime": "^6.23.0",
    "babel-preset-env": "^1.7.0",
    "babel-preset-stage-0": "^6.24.1",
    "babel-register": "^6.26.0",
    "cfonts": "^2.1.2",
    "chalk": "^2.4.1",
    "copy-webpack-plugin": "^4.5.1",
    "cross-env": "^5.1.6",
    "css-loader": "^0.28.11",
    "del": "^3.0.0",
    "electron": "22.3.18",
    "electron-builder": "22.10.5",
    "electron-debug": "^1.5.0",
    "electron-devtools-installer": "3.2.0",
    "file-loader": "^1.1.11",
    "html-webpack-plugin": "^3.2.0",
    "listr": "^0.14.3",
    "mini-css-extract-plugin": "0.4.0",
    "node-loader": "^0.6.0",
    "style-loader": "^0.21.0",
    "url-loader": "^1.0.1",
    "vue-html-loader": "^1.2.4",
    "vue-loader": "^15.2.4",
    "vue-style-loader": "^4.1.0",
    "vue-template-compiler": "^2.5.16",
    "webpack": "4.28.3",
    "webpack-cli": "3.2.1",
    "webpack-dev-server": "^3.1.4",
    "webpack-hot-middleware": "^2.22.2",
    "webpack-merge": "^4.1.3"
  }
}

增加.npmrc electron和electron-builder镜像,不然安装包能让你装到怀疑人生

ELECTRON_MIRROR=https://npmmirror.com/mirrors/electron/
ELECTRON_BUILDER_BINARIES_MIRROR=https://npmmirror.com/mirrors/electron-builder-binaries/

 npm可以设置代理,如果不会的,请自行搜索,这里就细讲了,有时候代理也不行就需要,在控制台,设置环境,不加这个,也能让你下载安装包下到怀疑人生,实在搞不了安装包问题的,可以私信我发node-modules

set http_proxy=http://127.0.0.1:7890 & set https_proxy=http://127.0.0.1:7890

vuex-electron 引用低版本的"electron-store": "^2.0.0",导致electron.remote报错,使得开启欢迎界面都打不开

得跑到node-modules下改写一下electron-store,还得安装@electron/remote这个包

'use strict';
const path = require('path');
const Conf = require('conf');

const moduleSource = process.type === 'browser' ? require('electron') : require('@electron/remote');
const { app, shell } = moduleSource;

class ElectronStore extends Conf {
    constructor(opts) {
        const defaultCwd = app.getPath('userData');

        opts = Object.assign({name: 'config'}, opts);

        if (opts.cwd) {
            opts.cwd = path.isAbsolute(opts.cwd) ? opts.cwd : path.join(defaultCwd, opts.cwd);
        } else {
            opts.cwd = defaultCwd;
        }

        opts.configName = opts.name;
        delete opts.name;
        super(opts);
    }

    openInEditor() {
        shell.openItem(this.path);
    }
}

module.exports = ElectronStore;

 改node-modules是不对的,但是先凑合启动起来再说

启动时,你会发现它非得安装一个vue-devtool插件,而且还是个manifest:2版本,最新的已经是manifest:3版本了,可以自己把安装包下载下来,但是这个会造成sandbox 环境报错,我觉得没啥用直接给注释了

另外 这个包也要升级,否则载入不正确

"electron-devtools-installer": "3.2.0"

插件的位置在,不过这里告诉了你怎么给渲染进程加插件的方法,windows想快速找这个路径,用everything搜索

C:\Users\senzo\AppData\Roaming\Electron\extensions\nhdogjmejiglipccpnnnanhbledajbpd

还有个热更新报错,这个去年已经讲了,这里就不再赘述,改了即可

另外就是electron 22版本,安全性加了很多,渲染进程默认不能调用electron api,必须开启contextIsolation:false,这个设置在webview中也有很大作用,这里不赘述

此外,上面说的那个@electron/remote库的使用,也必须将远程开起来,不然这个库无法使用

 主进程再配三段代码,才能在渲染进程中使用,这样看vuex-electron还是挺好的,这样主进程的数据变动,就可以影响所有的渲染进程了,以后还是可以探索探索的

最后再说一个,打开devtools,如果默认有菜单的话,就是不自动展开,必须把菜单关掉,才能每次dev时都打开

electron 这东西文档乱七八糟,版本迭代飞快,里面技术还多得不得了,但强大是真的强大,好用是真的好用,头疼是真的头疼,搞不好就内存泄漏了,问题多多,踩坑多多,后期我把代码整理号了,发到gitee上,再追加上来

相关推荐

  1. Electron-Builder 打包 Vue 项目指南

    2024-05-14 07:20:09       10 阅读
  2. electron兼容统信UOS系统过程中的

    2024-05-14 07:20:09       39 阅读
  3. ElectronElectron整合vue

    2024-05-14 07:20:09       28 阅读
  4. electron 如何升级版本

    2024-05-14 07:20:09       14 阅读
  5. electron实现静默打印(各种踩解决)

    2024-05-14 07:20:09       14 阅读
  6. vue+electron 自动更新

    2024-05-14 07:20:09       27 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-05-14 07:20:09       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-14 07:20:09       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-14 07:20:09       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-14 07:20:09       20 阅读

热门阅读

  1. 【APM】在Kubernetes中,使用Helm安装Grafana 9.5.1

    2024-05-14 07:20:09       9 阅读
  2. MySQL 查询库 和 表 占用空间大小的 语句

    2024-05-14 07:20:09       11 阅读
  3. VUE基础之scoped和TodList

    2024-05-14 07:20:09       8 阅读
  4. js通过音频链接获取音频时长

    2024-05-14 07:20:09       10 阅读
  5. 记录:卡尔曼滤波推导

    2024-05-14 07:20:09       11 阅读
  6. 卸载RabbitMq

    2024-05-14 07:20:09       9 阅读