# 項目搭建規范
## 一. 代碼規范
### 1.1. 集成editorconfig配置
EditorConfig 有助于為不同 IDE 編輯器上處理同一項目的多個開發人員維護一致的編碼風格。
```yaml
# http://editorconfig.org
root = true
[*] # 表示所有文件適用
charset = utf-8 # 設置文件字符集為 utf-8
indent_style = space # 縮進風格(tab | space)
indent_size = 2 # 縮進大小
end_of_line = lf # 控制換行類型(lf | cr | crlf)
trim_trailing_whitespace = true # 去除行首的任意空白字符
insert_final_newline = true # 始終在文件末尾插入一個新行
[*.md] # 表示僅 md 文件適用以下規則
max_line_length = off
trim_trailing_whitespace = false
```
VSCode需要安裝一個插件:EditorConfig for VS Code

### 1.2. 使用prettier工具
Prettier 是一款強大的代碼格式化工具,支持 JavaScript、TypeScript、CSS、SCSS、Less、JSX、Angular、Vue、GraphQL、JSON、Markdown 等語言,基本上前端能用到的文件格式它都可以搞定,是當下最流行的代碼格式化工具。
1.安裝prettier
```shell
npm install prettier -D
```
2.配置.prettierrc文件:
* useTabs:使用tab縮進還是空格縮進,選擇false;
* tabWidth:tab是空格的情況下,是幾個空格,選擇2個;
* printWidth:當行字符的長度,推薦80,也有人喜歡100或者120;
* singleQuote:使用單引號還是雙引號,選擇true,使用單引號;
* trailingComma:在多行輸入的尾逗號是否添加,設置為 `none`;
* semi:語句末尾是否要加分號,默認值true,選擇false表示不加;
```json
{
"useTabs": false,
"tabWidth": 2,
"printWidth": 80,
"singleQuote": true,
"trailingComma": "none",
"semi": false
}
```
3.創建.prettierignore忽略文件
```
/dist/*
.local
.output.js
/node_modules/**
**/*.svg
**/*.sh
/public/*
```
4.VSCode需要安裝prettier的插件

5.測試prettier是否生效
* 測試一:在代碼中保存代碼;
* 測試二:配置一次性修改的命令;
在package.json中配置一個scripts:
```json
"prettier": "prettier --write ."
```
### 1.3. 使用ESLint檢測
1.在前面創建項目的時候,我們就選擇了ESLint,所以Vue會默認幫助我們配置需要的ESLint環境。
2.VSCode需要安裝ESLint插件:

3.解決eslint和prettier沖突的問題:
安裝插件:(vue在創建項目時,如果選擇prettier,那么這兩個插件會自動安裝)
```shell
npm i eslint-plugin-prettier eslint-config-prettier -D
```
添加prettier插件:
```json
extends: [
"plugin:vue/vue3-essential",
"eslint:recommended",
"@vue/typescript/recommended",
"@vue/prettier",
"@vue/prettier/@typescript-eslint",
'plugin:prettier/recommended'
],
```
### 1.4. git Husky和eslint
雖然我們已經要求項目使用eslint了,但是不能保證組員提交代碼之前都將eslint中的問題解決掉了:
* 也就是我們希望保證代碼倉庫中的代碼都是符合eslint規范的;
* 那么我們需要在組員執行 `git commit ` 命令的時候對其進行校驗,如果不符合eslint規范,那么自動通過規范進行修復;
那么如何做到這一點呢?可以通過Husky工具:
* husky是一個git hook工具,可以幫助我們觸發git提交的各個階段:pre-commit、commit-msg、pre-push
如何使用husky呢?
這里我們可以使用自動配置命令:
```shell
npx husky-init && npm install
```
這里會做三件事:
1.安裝husky相關的依賴:

2.在項目目錄下創建 `.husky` 文件夾:
```
npx huksy install
```

3.在package.json中添加一個腳本:

接下來,我們需要去完成一個操作:在進行commit時,執行lint腳本:

這個時候我們執行git commit的時候會自動對代碼進行lint校驗。
### 1.5. git commit規范
#### 1.5.1. 代碼提交風格
通常我們的git commit會按照統一的風格來提交,這樣可以快速定位每次提交的內容,方便之后對版本進行控制。

但是如果每次手動來編寫這些是比較麻煩的事情,我們可以使用一個工具:Commitizen
* Commitizen 是一個幫助我們編寫規范 commit message 的工具;
1.安裝Commitizen
```shell
npm install commitizen -D
```
2.安裝cz-conventional-changelog,并且初始化cz-conventional-changelog:
```shell
npx commitizen init cz-conventional-changelog --save-dev --save-exact
```
這個命令會幫助我們安裝cz-conventional-changelog:

并且在package.json中進行配置:

這個時候我們提交代碼需要使用 `npx cz`:
* 第一步是選擇type,本次更新的類型
| Type | 作用 |
| -------- | ------------------------------------------------------------ |
| feat | 新增特性 (feature) |
| fix | 修復 Bug(bug fix) |
| docs | 修改文檔 (documentation) |
| style | 代碼格式修改(white-space, formatting, missing semi colons, etc) |
| refactor | 代碼重構(refactor) |
| perf | 改善性能(A code change that improves performance) |
| test | 測試(when adding missing tests) |
| build | 變更項目構建或外部依賴(例如 scopes: webpack、gulp、npm 等) |
| ci | 更改持續集成軟件的配置文件和 package 中的 scripts 命令,例如 scopes: Travis, Circle 等 |
| chore | 變更構建流程或輔助工具(比如更改測試環境) |
| revert | 代碼回退 |
* 第二步選擇本次修改的范圍(作用域)

* 第三步選擇提交的信息

* 第四步提交詳細的描述信息

* 第五步是否是一次重大的更改

* 第六步是否影響某個open issue

我們也可以在scripts中構建一個命令來執行 cz:

#### 1.5.2. 代碼提交驗證
如果我們按照cz來規范了提交風格,但是依然有同事通過 `git commit` 按照不規范的格式提交應該怎么辦呢?
* 我們可以通過commitlint來限制提交;
1.安裝 @commitlint/config-conventional 和 @commitlint/cli
```shell
npm i @commitlint/config-conventional @commitlint/cli -D
```
2.在根目錄創建commitlint.config.js文件,配置commitlint
```js
module.exports = {
extends: ['@commitlint/config-conventional']
}
```
3.使用husky生成commit-msg文件,驗證提交信息:
```shell
npx husky add .husky/commit-msg "npx --no-install commitlint --edit $1"
```
## 二. 第三方庫集成
### 2.1. vue.config.js配置
vue.config.js有三種配置方式:
* 方式一:直接通過CLI提供給我們的選項來配置:
* 比如publicPath:配置應用程序部署的子目錄(默認是 `/`,相當于部署在 `https://www.my-app.com/`);
* 比如outputDir:修改輸出的文件夾;
* 方式二:通過configureWebpack修改webpack的配置:
* 可以是一個對象,直接會被合并;
* 可以是一個函數,會接收一個config,可以通過config來修改配置;
* 方式三:通過chainWebpack修改webpack的配置:
* 是一個函數,會接收一個基于 [webpack-chain](https://github.com/mozilla-neutrino/webpack-chain) 的config對象,可以對配置進行修改;
```js
const path = require('path')
module.exports = {
outputDir: './build',
// configureWebpack: {
// resolve: {
// alias: {
// views: '@/views'
// }
// }
// }
// configureWebpack: (config) => {
// config.resolve.alias = {
// '@': path.resolve(__dirname, 'src'),
// views: '@/views'
// }
// },
chainWebpack: (config) => {
config.resolve.alias.set('@', path.resolve(__dirname, 'src')).set('views', '@/views')
}
}
```
### 2.2. vue-router集成
安裝vue-router的最新版本:
```shell
npm install vue-router@next
```
創建router對象:
```ts
import { createRouter, createWebHashHistory } from 'vue-router'
import { RouteRecordRaw } from 'vue-router'
const routes: RouteRecordRaw[] = [
{
path: '/',
redirect: '/main'
},
{
path: '/main',
component: () => import('../views/main/main.vue')
},
{
path: '/login',
component: () => import('../views/login/login.vue')
}
]
const router = createRouter({
routes,
history: createWebHashHistory()
})
export default router
```
安裝router:
```ts
import router from './router'
createApp(App).use(router).mount('#app')
```
在App.vue中配置跳轉:
```html
<template>
<div id="app">
<router-link to="/login">登錄</router-link>
<router-link to="/main">首頁</router-link>
<router-view></router-view>
</div>
</template>
```
### 2.3. vuex集成
安裝vuex:
```shell
npm install vuex@next
```
創建store對象:
```ts
import { createStore } from 'vuex'
const store = createStore({
state() {
return {
name: 'coderwhy'
}
}
})
export default store
```
安裝store:
```ts
createApp(App).use(router).use(store).mount('#app')
```
在App.vue中使用:
```html
<h2>{{ $store.state.name }}</h2>
```
### 2.4. element-plus集成
Element Plus,一套為開發者、設計師和產品經理準備的基于 Vue 3.0 的桌面端組件庫:
* 相信很多同學在Vue2中都使用過element-ui,而element-plus正是element-ui針對于vue3開發的一個UI組件庫;
* 它的使用方式和很多其他的組件庫是一樣的,所以學會element-plus,其他類似于ant-design-vue、NaiveUI、VantUI都是差不多的;
安裝element-plus
```shell
npm install element-plus
```
#### 2.4.1. 全局引入
一種引入element-plus的方式是全局引入,代表的含義是所有的組件和插件都會被自動注冊:
```js
import ElementPlus from 'element-plus'
import 'element-plus/lib/theme-chalk/index.css'
import router from './router'
import store from './store'
createApp(App).use(router).use(store).use(ElementPlus).mount('#app')
```
#### 2.4.2. 局部引入
也就是在開發中用到某個組件對某個組件進行引入:
```vue
<template>
<div id="app">
<router-link to="/login">登錄</router-link>
<router-link to="/main">首頁</router-link>
<router-view></router-view>
<h2>{{ $store.state.name }}</h2>
<el-button>默認按鈕</el-button>
<el-button type="primary">主要按鈕</el-button>
<el-button type="success">成功按鈕</el-button>
<el-button type="info">信息按鈕</el-button>
<el-button type="warning">警告按鈕</el-button>
<el-button type="danger">危險按鈕</el-button>
</div>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import { ElButton } from 'element-plus'
export default defineComponent({
name: 'App',
components: {
ElButton
}
})
</script>
<style lang="less">
</style>
```
但是我們會發現是沒有對應的樣式的,引入樣式有兩種方式:
* 全局引用樣式(像之前做的那樣);
* 局部引用樣式(通過babel的插件);
1.安裝babel的插件:
```shell
npm install babel-plugin-import -D
```
2.配置babel.config.js
```js
module.exports = {
plugins: [
[
'import',
{
libraryName: 'element-plus',
customStyleName: (name) => {
return `element-plus/lib/theme-chalk/${name}.css`
}
}
]
],
presets: ['@vue/cli-plugin-babel/preset']
}
```
但是這里依然有個弊端:
* 這些組件我們在多個頁面或者組件中使用的時候,都需要導入并且在components中進行注冊;
* 所以我們可以將它們在全局注冊一次;
```ts
import {
ElButton,
ElTable,
ElAlert,
ElAside,
ElAutocomplete,
ElAvatar,
ElBacktop,
ElBadge,
} from 'element-plus'
const app = createApp(App)
const components = [
ElButton,
ElTable,
ElAlert,
ElAside,
ElAutocomplete,
ElAvatar,
ElBacktop,
ElBadge
]
for (const cpn of components) {
app.component(cpn.name, cpn)
}
```
### 2.5. axios集成
安裝axios:
```shell
npm install axios
```
封裝axios:
```ts
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'
import { Result } from './types'
import { useUserStore } from '/@/store/modules/user'
class HYRequest {
private instance: AxiosInstance
private readonly options: AxiosRequestConfig
constructor(options: AxiosRequestConfig) {
this.options = options
this.instance = axios.create(options)
this.instance.interceptors.request.use(
(config) => {
const token = useUserStore().getToken
if (token) {
config.headers.Authorization = `Bearer ${token}`
}
return config
},
(err) => {
return err
}
)
this.instance.interceptors.response.use(
(res) => {
// 攔截響應的數據
if (res.data.code === 0) {
return res.data.data
}
return res.data
},
(err) => {
return err
}
)
}
request<T = any>(config: AxiosRequestConfig): Promise<T> {
return new Promise((resolve, reject) => {
this.instance
.request<any, AxiosResponse<Result<T>>>(config)
.then((res) => {
resolve((res as unknown) as Promise<T>)
})
.catch((err) => {
reject(err)
})
})
}
get<T = any>(config: AxiosRequestConfig): Promise<T> {
return this.request({ ...config, method: 'GET' })
}
post<T = any>(config: AxiosRequestConfig): Promise<T> {
return this.request({ ...config, method: 'POST' })
}
patch<T = any>(config: AxiosRequestConfig): Promise<T> {
return this.request({ ...config, method: 'PATCH' })
}
delete<T = any>(config: AxiosRequestConfig): Promise<T> {
return this.request({ ...config, method: 'DELETE' })
}
}
export default HYRequest
```
### 2.6. VSCode配置
```json
{
"workbench.iconTheme": "vscode-great-icons",
"editor.fontSize": 17,
"eslint.migration.2_x": "off",
"[javascript]": {
"editor.defaultFormatter": "dbaeumer.vscode-eslint"
},
"files.autoSave": "afterDelay",
"editor.tabSize": 2,
"terminal.integrated.fontSize": 16,
"editor.renderWhitespace": "all",
"editor.quickSuggestions": {
"strings": true
},
"debug.console.fontSize": 15,
"window.zoomLevel": 1,
"emmet.includeLanguages": {
"javascript": "javascriptreact"
},
"explorer.confirmDragAndDrop": false,
"workbench.tree.indent": 16,
"javascript.updateImportsOnFileMove.enabled": "always",
"editor.wordWrap": "on",
"path-intellisense.mappings": {
"@": "${workspaceRoot}/src"
},
"hediet.vscode-drawio.local-storage": "eyIuZHJhd2lvLWNvbmZpZyI6IntcImxhbmd1YWdlXCI6XCJcIixcImN1c3RvbUZvbnRzXCI6W10sXCJsaWJyYXJpZXNcIjpcImdlbmVyYWw7YmFzaWM7YXJyb3dzMjtmbG93Y2hhcnQ7ZXI7c2l0ZW1hcDt1bWw7YnBtbjt3ZWJpY29uc1wiLFwiY3VzdG9tTGlicmFyaWVzXCI6W1wiTC5zY3JhdGNocGFkXCJdLFwicGx1Z2luc1wiOltdLFwicmVjZW50Q29sb3JzXCI6W1wiRkYwMDAwXCIsXCIwMENDNjZcIixcIm5vbmVcIixcIkNDRTVGRlwiLFwiNTI1MjUyXCIsXCJGRjMzMzNcIixcIjMzMzMzM1wiLFwiMzMwMDAwXCIsXCIwMENDQ0NcIixcIkZGNjZCM1wiLFwiRkZGRkZGMDBcIl0sXCJmb3JtYXRXaWR0aFwiOjI0MCxcImNyZWF0ZVRhcmdldFwiOmZhbHNlLFwicGFnZUZvcm1hdFwiOntcInhcIjowLFwieVwiOjAsXCJ3aWR0aFwiOjExNjksXCJoZWlnaHRcIjoxNjU0fSxcInNlYXJjaFwiOnRydWUsXCJzaG93U3RhcnRTY3JlZW5cIjp0cnVlLFwiZ3JpZENvbG9yXCI6XCIjZDBkMGQwXCIsXCJkYXJrR3JpZENvbG9yXCI6XCIjNmU2ZTZlXCIsXCJhdXRvc2F2ZVwiOnRydWUsXCJyZXNpemVJbWFnZXNcIjpudWxsLFwib3BlbkNvdW50ZXJcIjowLFwidmVyc2lvblwiOjE4LFwidW5pdFwiOjEsXCJpc1J1bGVyT25cIjpmYWxzZSxcInVpXCI6XCJcIn0ifQ==",
"hediet.vscode-drawio.theme": "Kennedy",
"editor.fontFamily": "Source Code Pro, 'Courier New', monospace",
"editor.smoothScrolling": true,
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"workbench.colorTheme": "Atom One Dark",
"vetur.completion.autoImport": false,
"security.workspace.trust.untrustedFiles": "open",
"eslint.lintTask.enable": true,
"eslint.alwaysShowStatus": true,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
}
}
```
## 三. 接口文檔
https://documenter.getpostman.com/view/12387168/TzsfmQvw
baseURL的值:
```
http://152.136.185.210:5000
```
設置全局token的方法:
```js
const res = pm.response.json();
pm.globals.set("token", res.data.token);
```
- 文檔說明
- 開始
- linux
- 常用命令
- ps -ef
- lsof
- netstat
- 解壓縮
- 復制
- 權限
- 其他
- lnmp集成安裝
- supervisor
- 安裝
- supervisor進程管理
- nginx
- 域名映射
- 負載均衡配置
- lnmp集成環境安裝
- nginx源碼安裝
- location匹配
- 限流配置
- 日志配置
- 重定向配置
- 壓縮策略
- nginx 正/反向代理
- HTTPS配置
- mysql
- navicat創建索引
- 設置外網鏈接mysql
- navicat破解
- sql語句學習
- 新建mysql用戶并賦予權限
- php
- opcache
- 設計模式
- 在CentOS下安裝crontab服務
- composer
- 基礎
- 常用的包
- guzzle
- 二維碼
- 公共方法
- 敏感詞過濾
- IP訪問頻次限制
- CURL
- 支付
- 常用遞歸
- 數據排序
- 圖片相關操作
- 權重分配
- 毫秒時間戳
- base64<=>圖片
- 身份證號分析
- 手機號相關操作
- 項目搭建 公共處理函數
- JWT
- 系統函數
- json_encode / json_decode 相關
- 數字計算
- 數組排序
- php8
- jit特性
- php8源碼編譯安裝
- laravel框架
- 常用artisan命令
- 常用查詢
- 模型關聯
- 創建公共方法
- 圖片上傳
- 中間件
- 路由配置
- jwt
- 隊列
- 定時任務
- 日志模塊
- laravel+swoole基本使用
- 拓展庫
- 請求接口log
- laravel_octane
- 微信開發
- token配置驗證
- easywechart 獲取用戶信息
- 三方包
- webman
- win下熱更新代碼
- 使用laravel db listen 監聽sql語句
- guzzle
- 使用workman的httpCLient
- 修改隊列后代碼不生效
- workman
- 安裝與使用
- websocket
- eleticsearch
- php-es 安裝配置
- hyperf
- 熱更新
- 安裝報錯
- swoole
- 安裝
- win安裝swoole-cli
- google登錄
- golang
- 文檔地址
- 標準庫
- time
- 數據類型
- 基本數據類型
- 復合數據類型
- 協程&管道
- 協程基本使用
- 讀寫鎖 RWMutex
- 互斥鎖Mutex
- 管道的基本使用
- 管道select多路復用
- 協程加管道
- beego
- gin
- 安裝
- 熱更新
- 路由
- 中間件
- 控制器
- 模型
- 配置文件/conf
- gorm
- 初始化
- 控制器 模型查詢封裝
- 添加
- 修改
- 刪除
- 聯表查詢
- 環境搭建
- Windows
- linux
- 全局異常捕捉
- javascript
- 常用函數
- vue
- vue-cli
- 生產環境 開發環境配置
- 組件通信
- 組件之間通信
- 父傳子
- 子傳父
- provide->inject (非父子)
- 引用元素和組件
- vue-原始寫法
- template基本用法
- vue3+ts項目搭建
- vue3引入element-plus
- axios 封裝網絡請求
- computed 計算屬性
- watch 監聽
- 使用@符 代替文件引入路徑
- vue開發中常用的插件
- vue 富文本編輯
- nuxt
- 學習筆記
- 新建項目踩坑整理
- css
- flex布局
- flex PC端基本布局
- flex 移動端基本布局
- 常用css屬性
- 盒子模型與定位
- 小說分屏顯示
- git
- 基本命令
- fetch
- 常用命令
- 每次都需要驗證
- git pull 有沖突時
- .gitignore 修改后不生效
- 原理解析
- tcp與udp詳解
- TCP三次握手四次揮手
- 緩存雪崩 穿透 更新詳解
- 內存泄漏-內存溢出
- php_fpm fast_cgi cig
- redis
- 相關三方文章
- API對外接口文檔示范
- elaticsearch
- 全文檢索
- 簡介
- 安裝
- kibana
- 核心概念 索引 映射 文檔
- 高級查詢 Query DSL
- 索引原理
- 分詞器
- 過濾查詢
- 聚合查詢
- 整合應用
- 集群
- docker
- docker 簡介
- docker 安裝
- docker 常用命令
- image 鏡像命令
- Contrainer 容器命令
- docker-compose
- redis 相關
- 客戶端安裝
- Linux 環境下安裝
- uni
- http請求封裝
- ios打包
- 視頻縱向播放
- 日記
- 工作日記
- 情感日志
- 壓測
- ab
- ui
- thorui
- 開發規范
- 前端
- 后端
- 狀態碼
- 開發小組未來規劃