之前用 electron-vue 写过一个半成品的桌面端应用,但是是基于 Vue2 的,最近又想重写点桌面端应用,想要上 Vue3+TypeScript,于是便有了这篇文章总结下具体的搭建过程。
Vue Cli
Vue CLI 有一个插件vue-cli-plugin-electron-builder
,可以非常方便的搭建 electron 环境。
npm i @vue/cli -g
vue create my-app
根据自己项目的需求选择对应的依赖(例如 Babel,TS,Vuex 等等)
Vue CLI v5.0.3
? Please pick a preset: Manually select features
? Check the features needed for your project: Babel, TS, Vuex, CSS Pre-processors, Linter
? Choose a version of Vue.js that you want to start the project with 3.x
? Use class-style component syntax? Yes
? Use Babel alongside TypeScript (required for modern mode, auto-detected polyfills, transpiling JSX)? Yes
? Pick a CSS pre-processor (PostCSS, Autoprefixer and CSS Modules are supported by default): Sass/SCSS (with dart-sass)
? Pick a linter / formatter config: Prettier
? Pick additional lint features: Lint on save
? Where do you prefer placing config for Babel, ESLint, etc.? In package.json
? Save this as a preset for future projects? No
Vue CLI v5.0.3
✨ Creating project in F:\Electron\my-app.
🗃 Initializing git repository...
⚙️ Installing CLI plugins. This might take a while...
安装 vue-cli-plugin-electron-builder
Vue CLI Plugin Electron Builder (nklayman.github.io)
cd my-app
vue add electron-builder
安装过程中会提示你选择 Electron 的版本,选择最新版本即可
启动项目
npm run electron:serve
参考文章:Electron + Vue3 开发跨平台桌面应用【从项目搭建到打包完整过程】 - 掘金 (juejin.cn)
坑
error in ./src/background.ts
Module build failed (from ./node_modules/ts-loader/index.js):
TypeError: loaderContext.getOptions is not a function
我测试的时候,@vue/cli-plugin-typescript