🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## 环境准备 * node.js([官网地址](https://nodejs.org/en/)) * vue-cli 3.x ([官网地址](https://cli.vuejs.org/zh/guide/installation.html)) * 一个熟悉的代码编辑器 ## 初始化项目 vue create vue-electron-demo ```      Vue CLI v3.8.4     ? Please pick a preset: (Use arrow keys)       default (babel, eslint)      > Manually select features ``` 选择“Manually select features” (自定义安装)。 ``` ? Check the features needed for your project:      (*) Babel      ( ) TypeScript      ( ) Progressive Web App (PWA) Support      (*) Router      (*) Vuex      (*) CSS Pre-processors      (*) Linter / Formatter     >( ) Unit Testing      ( ) E2E Testing ``` 这里选择了常用的模块,请根据实际需求进行选择。 ``` ? Use history mode for router? (Requires proper server setup for index fallback      in production) (Y/n)  n ``` 如果选择了router,这里会询问是否使用history模式。 vue-router 默认使用hash模式(即通过url#hash来跳转页面),使用URL的hash来模拟一个完整的 URL,当URL改变时,页面不会重新加载。 如果使用history,URL就像正常的url,例如 yoursite.com/user/id ,比较好看。但是还需要后台配置支持。 这里我们选择“n”。 ``` ? Pick a CSS pre-processor (PostCSS, Autoprefixer and CSS Modules are supported by default):       Sass/SCSS (with dart-sass)       Sass/SCSS (with node-sass)     > Less       Stylus ``` 选择CSS预处理模块,这里我们使用“Less”。 ``` ? Pick a linter / formatter config:       ESLint with error prevention only       ESLint + Airbnb config     > ESLint + Standard config       ESLint + Prettier ``` 选择ESLint代码格式检查工具的配置,选择“ESLint + Standard config”,标准配置。 ``` ? Pick additional lint features: (Press to select, to toggle all, to invert selection)     >(*) Lint on save      ( ) Lint and fix on commit ``` Line on save表示在保存代码的时候,进行格式检查。 Lint and fix on commit表示在git commit的时候自动纠正格式。 这里只选择“Lint on save”。 ``` ? Where do you prefer placing config for Babel, ESLint, etc.?       In dedicated config files     > In package.json ``` 这里问把 babel, eslint 这些配置文件放哪? In dedicated config files 表示独立文件 In package.json 表示放在package.json里 这里选择“In package.json”。 ``` ? Save this as a preset for future projects? (y/N) N ``` 然后耐心等待项目安装完成 ## 安装electron 进入项目根目录,执行`vue add electron-builder` 此过程较慢,需耐心等待,或尝试手动安装可参考[链接](https://juejin.im/post/5d1abff7f265da1bb80c47e3#heading-7) 执行以下命令,开始编译APP,并启动开发环境APP: `npm run electron:serve` 编译成功后,就会出现开发环境的APP了。 ![](https://img.kancloud.cn/43/54/4354d6cd5906ac96b000c0fd3c9a3ef3_800x600.png) ## 打包项目 进入项目根目录,执行`npm run electron:build` 打包后的目录为:`demo-vue-electron\\dist\_electron`,其中demo-vue-electron Setup 0.1.0.exe为安装包,win-unpacked目录中有可直接执行的.exe ![](https://img.kancloud.cn/63/f3/63f355995f9511235477c3f78456e9cf_631x241.png) ## 配置项目 配置ESLint代码格式检查工具 在项目根目录下创建.eslintrc.js (注意文件名前面有个“.”) ``` module.exports = {       root: true,       env: {      node: true       },       'extends': [      'plugin:vue/essential',      '@vue/standard'       ],       rules: {     'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off',     // 不检测语句末尾的分号     'semi': ['off', 'always'],     // 强制缩进为2个空格     'indent': ['error', 2],     // 关闭函数名称跟括号之间的空格检测     'space-before-function-paren': 0,     // 忽略大括号内的空格     'object-curly-spacing': 0       },       parserOptions: {      parser: 'babel-eslint'       }     } ``` [更多配置参考](https://cloud.tencent.com/developer/section/1135570) 配置vue 在项目根目录下创建vue.config.js,粘贴以下代码: ``` const path = require('path');     function resolve (dir) {       return path.join(__dirname, dir);     }     module.exports = {       publicPath: './',       devServer: {     // can be overwritten by process.env.HOST     host: '0.0.0.0',       port: 8080       },       chainWebpack: config => {     config.resolve.alias       .set('@', resolve('src'))       .set('src', resolve('src'))       .set('common', resolve('src/common'))       .set('components', resolve('src/components'));       }     }; ``` [更多配置参考](https://cli.vuejs.org/zh/config/)