Vue 3 + Typescript + Vite2
初始化项目
yarn create vite <project name> --template vue-ts |
or
yarn create vite |
配置别名
安装 node TS 声明文件
yarn add --dev @types/node |
修改 vite.config.ts
// ... |
路由
安装依赖
yarn add --dev vue-router@next |
添加 views/home.vue
<template></template> |
创建 src/router/index.ts
import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router' |
修改 main.ts
添加 router
配置
// ... |
修改 App.vue
<template> |
状态管理
安装依赖
yarn add vuex@next --save |
创建 store/index.ts
import {createStore} from 'vuex'; |
修改 main.ts
添加 router
配置
// ... |
SASS
安装依赖
yarn add --dev sass |
添加 styles/index.scss styles/common.scss
// styles/index.scss |
修改 main.ts
引入
// ... |
jsx/tsx
安装插件
yarn add --dev @vitejs/plugin-vue-jsx |
修改 vite.config.ts
// ... |
修改 views/home.vue
<script lang="tsx"> |
代码规范
配置 .editorconfig
在项目根目录创建 .editorconfig
# Editor configuration, see http://editorconfig.org |
配置 eslint
安装依赖
yarn add --dev prettier eslint |
配置 prettier
在项目目录添加 .prettierrc
{ |
修改 package.json
// ... |
配置 ESlint
创建 .eslintrc.js
文件
npx eslint --init |
使用 husky
npx husky-init && yarn |
在 package.json
添加
"scripts": { |
修改 .husky/pre-commit
|
lint-staged
yarn add --dev lint-staged |
在 package.json
添加
"lint-staged": { |
jest
安装依赖
yarn add --dev jest @types/jest vue-jest@next @vue/test-utils@next |
添加 jest.config.ts
npx jest --init |
修改 jest.config.ts
module.exports = { |
解决报错
TypeError: Cannot destructure property 'config' of 'undefined' as it is undefined.
将 jest 和 ts-jest(babel-jest)版本从 27 改为 26
yarn add --dev ts-jest@26.5.6 jest@26.6.3 |
TS7016: Could not find a declaration file for module '@vue/test-utils'.
添加 <file name>.d.ts
declare module '@vue/test-utils'; |
解决测试文件引入样式无法识别的问题 1
在 jest.config.ts
添加
// ... |
并在项目目录下创建 __mocks__/fileMock.ts
和 __mocks__/styleMock.ts
提交规范
使用 commitizen
安装 commitizen
yarn global add commitizen |
初始化 commitizen
安装并初始化 commitizen
npx commitizen init cz-conventional-changelog --yarn --dev --exact |
自定义配置
npx commitizen init cz-customizable --yarn --dev --exact --force |
在项目根目录添加 .cz-config.js
module.exports = { |
使用 commitlint 验证 commit
yarn add --dev @commitlint/config-conventional @commitlint/cli |
创建 commitlint.config.js
module.exports = { extends: ['@commitlint/config-conventional'] } |
使用以下命令生成 commit-msg
npx husky add .husky/commit-msg "npx --no-install commitlint --edit $1" |
使用 commitizen
提交代码
git cz |
Axios
安装 axios
yarn add axios |
在根目录添加配置文件 .env.development
VITE_BASE_API=/api |
添加 env.d.ts
声明文件
// eslint-disable-next-line no-unused-vars |
创建 src/utils/request.ts
import Axios from 'axios' |