# vue3-template-cli **Repository Path**: theGreatWallCCG/vue3-template-cli ## Basic Information - **Project Name**: vue3-template-cli - **Description**: 常规的vue3全家桶基础模板,vue3 + ts + vite + vue-router + pinia 在此基础上为解决团队开发代码风格不统一,从开发阶段到git提交,引入了eslint、prettier、husky、commitizen、cz-customizable进行代码规范 可依据此项目从零开发,也可按照此项目教程在原有项目上进行优化 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 2 - **Forks**: 1 - **Created**: 2023-08-02 - **Last Updated**: 2023-09-25 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Vue 3 + TypeScript + Vite 常规的vue3全家桶基础模板,vue3 + ts + vite + vue-router + pinia 在此基础上为解决团队开发代码风格不统一,从开发阶段到git提交,引入了eslint、prettier、husky、commitizen、cz-customizable进行代码规范 可依据此项目从零开发,也可按照此项目教程在原有项目上进行优化 - 通过vite创建包含ts的vue项目 - 引入vue-router、pinia完善全家桶 - 引入eslint、prettier支持,添加.editorconfig,引入vite-plugin-checker优化错误提示 - 添加husky、lint-staged、commitizen、cz-customizable支持规范代码风格 - 添加windicss、postcss [github](https://github.com/BenjaminCCG/vue3-template-cli.git) [国内gitee](https://gitee.com/theGreatWallCCG/my-vue-cli) ## 1.项目初始化 ```js pnpm create vite my-vue-cli --template vue-ts cd my-vue-cli pnpm install pnpm run dev ``` ## 2.引入vue-router、pinia ```js pnpm add vue-router@4 pinia @types/node ``` tsconfig.json添加如下配置 ```js { "compilerOptions":{ ..., "baseUrl": "./", "paths": { "@/*": ["src/*"] }, } } ``` vite.config.ts引入 ```js import { resolve } from "path"; ..., resolve: { alias: { "@": resolve(__dirname, "src"), }, }, ``` 新建pages目录下,建立home/index.vue文件测试 ![image.png](https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/d831f93055e04578a5b4efaf930a22e8~tplv-k3u1fbpfcp-watermark.image?) 新建router目录,建立index.ts ```js import { createWebHashHistory, createRouter } from 'vue-router'; import Home from '@/pages/home/index.vue'; const router = createRouter({ history: createWebHashHistory(), routes: [ { path: '/', redirect: '/home' }, { path: '/home', name: 'Home', component: Home } ] }); export default router; ``` 新建store目录,建立user.ts pinia模块 ```js import { defineStore } from 'pinia'; export const useUserStore = defineStore('user', { state: () => ({ name: 'vben' }), getters: {}, actions: {} }); ``` 修改app.vue组件设置路由入口 ```js ``` main.ts引入安装router、pinia ```js import { createApp } from 'vue'; import App from './App.vue'; import { createPinia } from 'pinia'; import router from './router'; const app = createApp(App); app.use(createPinia()).use(router).mount('#app'); ``` 修改home/index.vue代码 ```js ``` 此时访问页面可见,已正常引入 ![image.png](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/616549c485604f52b9645e10a9067234~tplv-k3u1fbpfcp-watermark.image?) ## 3.安装eslint、prettier、vite-plugin-checker 首页完成eslint的安装配置 ```js pnpm add -D eslint npx eslint --init ? How would you like to use ESLint? ... To check syntax only To check syntax and find problems > To check syntax, find problems, and enforce code style ---------------------------------------------------------------- 选择强制代码风格 √ How would you like to use ESLint? · style ? What type of modules does your project use? ... > JavaScript modules (import/export) CommonJS (require/exports) None of these - --------------------------------------------------------------- 选择ESM规范 √ How would you like to use ESLint? · style √ What type of modules does your project use? · esm ? Which framework does your project use? ... React > Vue.js None of these ---------------------------------------------------------------- 选择VUE框架 √ How would you like to use ESLint? · style √ What type of modules does your project use? · esm √ Which framework does your project use? · vue ? Does your project use TypeScript? » No / Yes ---------------------------------------------------------------- 使用TS yes √ How would you like to use ESLint? · style √ What type of modules does your project use? · esm √ Which framework does your project use? · vue √ Does your project use TypeScript? · No / Yes ? Where does your code run? ... (Press to select, to toggle all, to invert selection) √ Browser √ Node ---------------------------------------------------------------- 空格增选Node 回车 √ How would you like to use ESLint? · style √ What type of modules does your project use? · esm √ Which framework does your project use? · vue √ Does your project use TypeScript? · No / Yes √ Where does your code run? · browser, node ? How would you like to define a style for your project? ... Use a popular style guide > Answer questions about your style ---------------------------------------------------------------- 用过A&Q来配置规则 √ How would you like to define a style for your project? · prompt ? What format do you want your config file to be in? ... > JavaScript YAML JSON ---------------------------------------------------------------- 配置文件使用js文件 What style of indentation do you use? · tab √ What quotes do you use for strings? · double √ What line endings do you use? · unix √ Do you require semicolons? · YES The config that you‘ve selected requires the following dependencies: eslint-plugin-vue@latest @typescript-eslint/eslint-plugin@latest @typescript-eslint/parser@latest ---------------------------------------------------------------- 选择yes现在立即初始化配置文件 Would you like to install them now? · YES Which package manager do you want to use? ... npm yarn > pnpm ---------------------------------------------------------------- 包管理器选择pnpm ``` 安装prettier ```js pnpm add prettier eslint-config-prettier eslint-plugin-prettier ``` 添加.prettierrc.cjs ```js module.exports = { // 一行最多 120 字符 printWidth: 120, // 使用 2 个空格缩进 tabWidth: 2, // 不使用缩进符,而使用空格 useTabs: false, // 行尾需要有分号 semi: true, // 使用单引号 singleQuote: true, // 对象的 key 仅在必要时用引号 quoteProps: 'as-needed', // jsx 不使用单引号,而使用双引号 jsxSingleQuote: false, // 末尾需要有逗号 trailingComma: 'none', // 大括号内的首尾需要空格 bracketSpacing: true, // jsx 标签的反尖括号需要换行 bracketSameLine: false, // 箭头函数,只有一个参数的时候,也需要括号 arrowParens: 'always', // 每个文件格式化的范围是文件的全部内容 rangeStart: 0, rangeEnd: Infinity, // 不需要写文件开头的 @prettier requirePragma: false, // 不需要自动在文件开头插入 @prettier insertPragma: false, // 使用默认的折行标准 proseWrap: 'preserve', // 根据显示样式决定 html 要不要折行 htmlWhitespaceSensitivity: 'css', // vue 文件中的 script 和 style 内不用缩进 vueIndentScriptAndStyle: false, // 换行符使用 lf endOfLine: 'lf', // 格式化嵌入的内容 embeddedLanguageFormatting: 'auto', // html, vue, jsx 中每个属性占一行 singleAttributePerLine: false }; ``` #### 使用 AlloyTeam 的 ESLint 配置 此时ESLint 原生的规则和 `@typescript-eslint/eslint-plugin` 的规则太多了,会产生许多报错,而且原生的规则有一些在 TypeScript 中支持的不好,需要禁用掉。 ```js pnpm add eslint-config-alloy @babel/eslint-parser ``` .eslintrc.cjs修改为 ```js module.exports = { extends: ['alloy', 'alloy/vue', 'alloy/typescript'], parser: 'vue-eslint-parser', parserOptions: { parser: { js: '@babel/eslint-parser', jsx: '@babel/eslint-parser', ts: '@typescript-eslint/parser', tsx: '@typescript-eslint/parser' // Leave the template parser unspecified, so that it could be determined by `