Vue3项目开发基本流程(一)
本文方法只在相关版本下测试使用,未做全面测试,也不保证其他版本可用。
Vue3 项目开发基本流程(一) 项目框架搭建
安装 VueCli
1 | npm install -g @vue/cli |
项目框架搭建
创建项目
1 | vue create vue3-project |

配置 .editorconfig
- VSCode 安装 EditorConfig for VS Code 插件
- 项目根目录右键点击 Generate .editorconfig,生成.editorconfig 文件
- 配置.editorconfig 文件
1 | EditorConfig is awesome: https://EditorConfig.org |
配置 prettier
安装 prettier
- prettier,强大的代码格式化工具。
1 | npm install prettier -D --save-exact |
prettier 相关配置
- 新建.prettierrc 文件
- 配置.prettierrc
1
2
3
4
5
6
7
8{
"useTabs": false, //使用tab缩进还是空格缩进
"tabWidth": 2, //tab空格的字符长度为2
"printWidth": 80, //每一行字符最大的长度
"singleQuote": false, //单引号or双引号
"trailingComma": "none", //多行输入的尾逗号是否添加
"semi": true //语句末尾是否加分号
}
- 配置.prettierrc
- 新建.prettierignore 文件,忽略某个文件使用 prettier 进行代码格式化
- 配置.prettierignore 文件
1
2
3
4
5
6
7
8
9/dist/*
.local
.output.js
/node_modules/**
**/*.svg
**/*.sh
/public/*- VSCode 安装 prettier 插件
- 手动配置 package.json prettier 脚本
1
2
3"scripts": {
"prettier": "prettier --write"
}
- 手动配置 package.json prettier 脚本
- 在新建的项目启动前,执行 prettier 代码格式化命令
1
npm run prettier
- 在新建的项目启动前,执行 prettier 代码格式化命令
配置 ESlint
- VueClii 项目已经默认配置 ESlint 环境
- VSCode 安装 ESlint 插件
- 解决 ESlint 和 prettier 冲突问题
1
2安装两个插件(vuecli创建项目时,选择prettier就会自动安装)
npm i eslint-plugin-prettier eslint-config-prettier -D
- 解决 ESlint 和 prettier 冲突问题
- .eslintrc.js 配置
1
2
3
4
5
6
7
8extends: [
"plugin:vue/vue3-essential",
"eslint:recommended",
"@vue/typescript/recommended",
"plugin:prettier/recommended",
"@vue/prettier",
"@vue/prettier/@typescript-eslint"
]
- .eslintrc.js 配置
- 配置 git 提交规范,使其符合 ESlint 要求
- git Husky 相关配置
- 安装 husky,husky 是一个 git hook 工具,可以触发 git 提交的各个阶段,pre-commit、commit-msg、pre-push
1
npx husky-init && npm install
- 安装完成后会自动在 package.json 脚本中生成 husky 脚本
1
2
3"scripts": {
"prepare": "husky install"
} - 安装完成后会自动在项目根目录生成.husky 文件夹;需要手动在 .husky/pre-commit 文件中配置
1
2npm run lint
这样配置完成后,在执行git commit时 会自动进行ESlint校验
- npx cz 提交规范配置
- 安装 commitizen,commitizen 是在 git commit 提交 meaasge 的文本风格规范
1
npm i commitizen -D
- 安装 cz-conventional-changelog,初始化 cz-conventional-changelog
1
npx commitizen init cz-conventional-changelog --save-dev --save-exact
- 安装和初始化 cz-conventional-changelog 之后:
- 自动在 package.json 脚本中生成
1
2
3
4
5"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
}
- 自动在 package.json 脚本中生成
- 安装完成后,需要手动配置 package.json commit 脚本,代码提交时在终端执行 npx cz 或者 npm run commmit
1
2
3"scripts": {
"commit": "cz"
}
- git commit 提交规范配置
- 安装@commitlint/config-conventional 和 @commitlint/cli
1
npm install --save-dev @commitlint/config-conventional @commitlint/cli -D
- 项目根目录创建 commitlint.config.js,配置
1
2
3module.exports = {
extends: ["@commitlint/config-conventional"]
} - 在终端执行以下命令,使用 husky 生成提交信息 commit-msg,验证提交信息规范
1
npx husky add .husky/commit-msg "npx --no-install commitlint --edit $1"
- 上述命令执行完成后,项目根目录自动生成.husky/commit-msg 文件,在 commit-msg 中配置
1
npx --no-install commitlint --edit
配置 vue.config.js
1 | const path = require("path") |
配置 vue-router
安装 vue-router
1 | npm i vue-router |
新建 router/index.ts
1 | //创建router对象 |
挂载 vue-router
1 | # 在main.ts中挂载router |
配置 vuex
安装 vuex
1 | npm i vuex |
新建 store/index.ts
1 | //创建store实例 |
挂载 vuex
1 | import store from "./store" |
配置全局 CSS
安装 normalize.css
配置 CSS
- 手动创建 src/assets/css/base.less
- 手动创建 src/assets/css/index.less
1 | @import url("./base.less"); |
- 在 main.ts 中引用 css
1 | import "normalize.css" |
POSTCSS 浏览器适配
- 安装 postcss-px-to-viewport-8-plugin
1 | 将px转换为vw/vh |
- 项目根目录创建 postcss.config.js,配置
1 | module.exports = { |
第三方 API 接口跨域代理配置
vue.config.js 配置代理
1 | const { defineConfig } = require("@vue/cli-service") |
接口跨域测试错误处理
- 404
1 | //以axios网络请求为例,需要配置 |
- 403
1 | <!-- 在public/index.html配置,所有请求不发送 referrer,绕过防盗链 --> |
配置环境
- 创建 .env.development .env.production 文件
1 | .env.development文件 |
axios 封装
安装 axios
1 | npm i axios |
配置与封装 axios
- 新建 axios 配置文件, src/service/request/config.ts
1 | let BASE_URL = "" |
- 新建类型文件 src/service/request/type.ts
1 | let BASE_URL = "" |
- 新建类型文件 src/service/request/type.ts
1 | import { AxiosRequestConfig, AxiosResponse } from "axios" |
- 新建 axios 请求文件, src/service/request/request.ts
1 | import axios from "axios" |
- 新建 axios 封装导出文件, src/service/index.ts
1 | import MyRequest from "./request/request" |
- 项目引用 axios
1 | import { myRequestInstance } from "../index" |
配置 element-plus
安装 element-plus
1 | npm i element-plus |
全局引用 element-plus
1 | import ElementPlus from "element-plus" |
局部引用 element-plus
- 单组件文件中按需引用
1 | <script lang="ts"> |
按需引用多个组件,需要自动配置
- 安装 bable-plugin-import
1
npm i babel-plugin-import -D
- 配置 babel.config.js
1
2
3
4
5
6
7
8
9
10
11module.exports = {
plugins: [
"import",
{
libraryName: "element-plus",
customStyleName: (name) => {
return `element-plus/lib/theme-chalk/${name}.css`
}
}
]
}- 单组件文件中配置
1
2
3
4
5
6
7
8
9
10<script lang="ts">
import { ElButton } from "elemeny-plus"
import "element-plus/dist/index.css"
export default defineComponent({
components: {
ElButton
}
})
</script>按需引用,全局注册
- 安装 bable-plugin-import 配置 babel.config.js,跟自动配置一样
- main.ts 文件中引用
1
2
3import { ElButton } from "elemeny-plus"
import "element-plus/dist/index.css"
app.component(ElButton.name, ElButton) - 全局注册多个组件
1
2
3
4
5
6import { ElButton, ElForm, ElInput } from "elemeny-plus"
import "element-plus/dist/index.css"
const components = [ElButton, ElForm, ElInput]
for (const component of components) {
app.component(component.name, component)
}
按需引用,全局注册,抽取封装
安装 bable-plugin-import 配置 babel.config.js,跟自动配置一样
创建文件 src/global/register-element.ts
1
2
3
4
5
6
7
8
9import { App } from "vue"
import { ElButton } from "elemeny-plus"
import "element-plus/dist/index.css"
const components = [ElButton, ElForm, ElInput]
export default function (app: App): void {
for (const component of components) {
app.component(component.name, component)
}
}创建文件 src/global/index.ts
1
2
3
4
5
6
7import { App } from "vue"
import registerElement from "./register-element"
export function registerApp(app: App): void {
//registerElement(app)
app.use(registerElement)
}main.ts 文件中引用
1
2
3import { registerApp } from "./global"
//registerApp(app)
app.use(registerApp)
配置 vant
安装 vant
1 | npm i vant |
按需引用
- 安装 babel.config.js
1 | npm i babel-plugin-import -D |
- 配置 babel.config.js
1 | { |
- 单文件组件引用
1 | <template> |
项目 package.json
1 | { |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 浮生二三四!