= [
{
path: '/login',
name: 'Login',
meta: {
title: '登录',
keepAlive: true,
requireAuth: false
},
component: () => import('@/pages/login.vue')
},
{
path: '/',
name: 'Index',
meta: {
title: '首页',
keepAlive: true,
requireAuth: true
},
component: () => import('@/pages/index.vue')
}
]
const router = createRouter({
history: createWebHistory(),
routes
});
export default router;
```
2. 在 main.ts 中引入并使用
```typescript
import { createApp } from 'vue'
import App from './App.vue'
import store from './store'
import router from '@/router';
// 创建vue实例
const app = createApp(App);
app.use(router);
// 挂载实例
app.mount('#app');
```
3. 修改 App.vue
```typescript
```
# 5. 集成 `vueuse`
`VueUse` 是一个基于 `Composition API` 的实用函数集合。
## 安装
```
pnpm i @vueuse/core
```
## 使用
1. 创建一个新的 src/page/vueUse.vue 页面来做一个简单的 demo
```typescript
测试 vueUse 的鼠标坐标
Mouse: {{x}} x {{y}}
```
useMouse 只是 vueuse 的一个最基本的函数库,还有许多,总会有一个适合你;
更多函数官方文档:[链接](https://vueuse.org/)
# 6. CSS 的集成
## 方案一:原生 css variable 新特性:
原生支持,不需要第三方插件,具体使用文档可 [查看](https://developer.mozilla.org/zh-CN/docs/Web/CSS/var)
1. 新建文件 src/styles/index.css
```css
:root {
--main-bg-color: pink;
}
body {
background-color: var(--main-bg-color);
}
```
注:还可以增加 PostCSS 配置,(任何受 [postcss-load-config](https://github.com/postcss/postcss-load-config) 支持的格式,例如 `postcss.config.js` ),它将会自动应用于所有已导入的 CSS。
## 方案二:scss 或 less:
1. 安装
```bash
# .scss and .sass
pnpm add -D sass
# .less
pnpm add -D less
```
2. 使用在 .vue 文件模板中
```typescript
// .scss
欢迎使用 scss
// .less
欢迎使用 less
```
# 7. 集成 `axios`
`axios` 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中。
## 安装
```bash
pnpm i axios
```
## 使用:
1. 新建 src/utils/axios.ts
```typescript
import axios, { AxiosResponse, AxiosRequestConfig } from 'axios';
const service = axios.create();
// Request interceptors
service.interceptors.request.use(
(config: AxiosRequestConfig) => {
// do something
return config;
},
(error: any) => {
Promise.reject(error);
}
);
// Response interceptors
service.interceptors.response.use(
async (response: AxiosResponse) => {
// do something
},
(error: any) => {
// do something
return Promise.reject(error);
}
);
export default service;
```
2. 在页面中使用即可
```typescript
```
## 封装请求参数和响应数据的所有 api (可选项)
1. 新建 `src/api/index.ts`
```typescript
import * as login from './module/login';
import * as index from './module/index';
export default Object.assign({}, login, index);
```
2. 新建 `src/api/module/login.ts` 和 `src/api/module/index.ts`
```typescript
import request from '@/utils/axios';
/**
* 登录
*/
interface IResponseType {
code?: number;
status: number;
msg: string;
data: P;
}
interface ILogin {
token: string;
expires: number;
}
export const login = (username: string, password: string) => {
return request>({
url: '/api/auth/login',
method: 'post',
data: {
username,
password
}
});
};
```
3. 由于使用了 typescript,所以需新增 `src/types/shims-axios.d.ts`
```typescript
import { AxiosRequestConfig } from 'axios';
/**
* 自定义扩展axios模块
* @author Maybe
*/
declare module 'axios' {
export interface AxiosInstance {
(config: AxiosRequestConfig): Promise;
request(config: AxiosRequestConfig): Promise;
get(url: string, config?: AxiosRequestConfig): Promise;
delete(url: string, config?: AxiosRequestConfig): Promise;
head(url: string, config?: AxiosRequestConfig): Promise;
post(url: string, data?: any, config?: AxiosRequestConfig): Promise;
put(url: string, data?: any, config?: AxiosRequestConfig): Promise;
patch(url: string, data?: any, config?: AxiosRequestConfig): Promise;
}
}
```
4. 在 `src/pages/request.vue` 页面中使用
```typescript
```
# 8. css 的 UI 样式库
> 可选很多,根据自己项目的需求去进行选择即可
**注意:UI 库一般需要按需引入(下面以 `element-plus` 为例)**
1. 安装 `vite-plugin-style-import`
```bash
pnpm i vite-plugin-style-import --save-dev
```
2. 修改 `vite.config.ts`
```typescript
...
import styleImport from 'vite-plugin-style-import'
export default defineConfig({
...
plugins: [
vue(),
styleImport({
libs: [
{
libraryName: 'element-plus',
esModule: true,
resolveStyle: (name) => {
return `element-plus/lib/theme-chalk/${name}.css`;
},
ensureStyleFile: true // 忽略文件是否存在, 导入不存在的CSS文件时防止错误。
}
]
})
],
...
})
```
# 9. 使用 [commitizen](https://github.com/commitizen/cz-cli) 规范git提交
为了使团队多人协作更加的规范,所以需要每次在 git 提交的时候,做一次硬性规范提交,规范 git 的提交信息
## 安装 `commitizen` (交互式提交 + 自定义提示文案 + Commit规范)
1. 安装
```bash
pnpm install -D commitizen cz-conventional-changelog @commitlint/config-conventional @commitlint/cli commitlint-config-cz cz-customizable
```
2. 配置 `package.json`
```json
{
...
"scripts": {
"commit:comment": "引导设置规范化的提交信息",
"commit":"git-cz",
},
"config": {
"commitizen": {
"path": "node_modules/cz-customizable"
}
},
...
}
```
3. 新增配置 `commitlint.config.js`
```javascript
module.exports = {
extends: ['@commitlint/config-conventional', 'cz'],
rules: {
'type-enum': [
2,
'always',
[
'feature', // 新功能(feature)
'bug', // 此项特别针对bug号,用于向测试反馈bug列表的bug修改情况
'fix', // 修补bug
'ui', // 更新 ui
'docs', // 文档(documentation)
'style', // 格式(不影响代码运行的变动)
'perf', // 性能优化
'release', // 发布
'deploy', // 部署
'refactor', // 重构(即不是新增功能,也不是修改bug的代码变动)
'test', // 增加测试
'chore', // 构建过程或辅助工具的变动
'revert', // feat(pencil): add ‘graphiteWidth’ option (撤销之前的commit)
'merge', // 合并分支, 例如: merge(前端页面): feature-xxxx修改线程地址
'build', // 打包
],
],
// 格式 小写
'type-case': [2, 'always', 'lower-case'],
// 不能为空
'type-empty': [2, 'never'],
// 范围不能为空
'scope-empty': [2, 'never'],
// 范围格式
'scope-case': [0],
// 主要 message 不能为空
'subject-empty': [2, 'never'],
// 以什么为结束标志,禁用
'subject-full-stop': [0, 'never'],
// 格式,禁用
'subject-case': [0, 'never'],
// 以空行开头
'body-leading-blank': [1, 'always'],
'header-max-length': [0, 'always', 72],
},
};
```
4. 自定义提示则添加 `.cz-config.js`
```javascript
module.exports = {
types: [
{value: 'feature', name: 'feature: 增加新功能'},
{value: 'bug', name: 'bug: 测试反馈bug列表中的bug号'},
{value: 'fix', name: 'fix: 修复bug'},
{value: 'ui', name: 'ui: 更新UI'},
{value: 'docs', name: 'docs: 文档变更'},
{value: 'style', name: 'style: 代码格式(不影响代码运行的变动)'},
{value: 'perf', name: 'perf: 性能优化'},
{value: 'refactor', name: 'refactor: 重构(既不是增加feature,也不是修复bug)'},
{value: 'release', name: 'release: 发布'},
{value: 'deploy', name: 'deploy: 部署'},
{value: 'test', name: 'test: 增加测试'},
{value: 'chore', name: 'chore: 构建过程或辅助工具的变动(更改配置文件)'},
{value: 'revert', name: 'revert: 回退'},
{value: 'build', name: 'build: 打包'}
],
// override the messages, defaults are as follows
messages: {
type: '请选择提交类型:',
customScope: '请输入您修改的范围(可选):',
subject: '请简要描述提交 message (必填):',
body: '请输入详细描述(可选,待优化去除,跳过即可):',
footer: '请输入要关闭的issue(待优化去除,跳过即可):',
confirmCommit: '确认使用以上信息提交?(y/n/e/h)'
},
allowCustomScopes: true,
skipQuestions: ['body', 'footer'],
subjectLimit: 72
};
```
5. 交互界面测试

- 到目前只是规范了 git 的提交信息,我们对提交前代码的检查还没有做到位,例如 ESLint、Prettier,毕竟谁都会有疏忽的时候,
- 那么现在我们的 husky 就闪亮登场了
## 安装 husky
1. 安装
```bash
# 1.安装
pnpm i husky lint-staged -D
# 2.生成 .husky 的文件夹
npx husky install
# 3.添加 hooks,会在 .husky 目录下生成一个 pre-commit 脚本文件
npx husky add .husky/pre-commit "npx --no-install lint-staged"
# 4.添加 commit-msg
npx husky add .husky/commit-msg 'npx --no-install commitlint --edit "$1"'
# 5. 使用 `git commit -m "message"` 就会看到 hook 生效了。
```
2. 添加配置 `package.json`
```json
{
...
"lint-staged": {
"*.{js,ts}": [
"npm run eslint",
"npm run prettier"
]
}
...
}
```
## 提交日志(可选)
- [standard-version](https://github.com/conventional-changelog/standard-version) 或者 [conventional-changelog](https://github.com/conventional-changelog/conventional-changelog)
`靓仔靓女们`,都看到这里了,要不点个赞再走呗 🌹🌹🌹