前端框架搭建——从零到一搭建一个高颜值Vue3后台管理系统(一)
<h1 style="color: black; text-align: left; margin-bottom: 10px;">项目展示</h1>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">登录</h1>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-axegupay5k/7c73d6146f5c4983b7b2c135f77f554d~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728744670&x-signature=8sP6JT315hn363QqDPq5%2FFkYFWk%3D" style="width: 50%; margin-bottom: 20px;"></div>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">首页</h1>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-twdt4qpehh/1f074dd1ed4d4104a41d3f93e4abb6d9~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728744670&x-signature=hsQFKw0%2BdLTH75H%2Bpksrpwo5kMw%3D" style="width: 50%; margin-bottom: 20px;"></div>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">列表</h1>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-twdt4qpehh/f9a4d04a10054784bc89bbc4878adec5~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728744670&x-signature=CvXhbEwyeiNpRlHkd35YvH6kxO4%3D" style="width: 50%; margin-bottom: 20px;"></div>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">深色侧边栏</h1>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-twdt4qpehh/10956c4907e945fe93d0c011e8690af1~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728744670&x-signature=u%2BK1eevmisVZp42cZgN9f3EpZHQ%3D" style="width: 50%; margin-bottom: 20px;"></div>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">暗黑主题</h1>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-twdt4qpehh/79e3e0780ddc45ea9210a43ba0aef98c~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728744670&x-signature=%2FVyVVizsABferWihDWhyJyvsQN8%3D" style="width: 50%; margin-bottom: 20px;"></div>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">移动端</h1>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-twdt4qpehh/6914bc80fe2c4695a0cd37cc7459e84c~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728744670&x-signature=R0JOMDzpRsaffNGU01w40aSrH1g%3D" style="width: 50%; margin-bottom: 20px;"></div>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">关于</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">为何</span>我要写这个项目呢,后台业务在项目中是很<span style="color: black;">平常</span>的,而市面上的后台框架其实都很好,<span style="color: black;">然则</span><span style="color: black;">非常多</span>时候感觉封装的很深,<span style="color: black;">因此</span>是想要自己搭建一个框架方便后面自己<span style="color: black;">运用</span>,<span style="color: black;">亦</span><span style="color: black;">能够</span><span style="color: black;">经过</span>这种方式分享下经验和心得,<span style="color: black;">日前</span>的话这个项目不是一个赶工的项目,预计会<span style="color: black;">连续</span>更新产出。</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">技术栈</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">此项目<span style="color: black;">触及</span>的技术栈<span style="color: black;">重点</span>有</p>Vue3Element PlusPiniaUnocssAxiosTypeScript├── <span style="color: black;">public</span> <span style="color: black;"># 不参与编译的资源文件</span>
├── build <span style="color: black;"># 构建时的<span style="color: black;">有些</span>插件配置</span>
├── src <span style="color: black;"># 主程序目录</span>
│ ├── api <span style="color: black;"># api请求</span>
│ ├── assets <span style="color: black;"># 资源文件</span>│ │ ├── svg<span style="color: black;"># svg图标</span>
│ │ └── images <span style="color: black;"># <span style="color: black;">照片</span>资源</span>
│ ├── components <span style="color: black;"># 全局公共组件</span>
│ ├── config <span style="color: black;"># 主题<span style="color: black;">关联</span>配置</span>
│ ├── directives <span style="color: black;"># 自定义指令</span>
│ ├── hooks <span style="color: black;"># hooks</span>
│ ├── layout <span style="color: black;"># 页面结构组件</span>│ ├── locales<span style="color: black;"># 国际化</span>
│ ├── plugins <span style="color: black;"># 加载插件与静态</span>
│ ├── router <span style="color: black;"># 路由目录</span>
│ ├── store <span style="color: black;"># pinia 配置</span>
│ ├── style <span style="color: black;"># 样式目录</span>
│ ├── utils <span style="color: black;"># 工具类</span>
│ ├── views <span style="color: black;"># 页面</span>
│ ├── APP.vue <span style="color: black;"># App.vue</span>
│ └── main.ts <span style="color: black;"># main.ts</span>
├── types <span style="color: black;"># 全局类型</span>
├── .editorconfig <span style="color: black;"># 代码规范</span>
├── .prettierrc.js <span style="color: black;"># 代码规范</span>
├── .eslintrc <span style="color: black;"># 代码规范</span>
├── tsconfig.json <span style="color: black;"># ts配置</span>
├── uno.config.ts <span style="color: black;"># unocss配置</span>├── commitlint.config.js<span style="color: black;"># 提交信息</span>
├── vite.config.ts <span style="color: black;"># vite配置</span>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">初始化配置</h1>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">初始化项目</h1><span style="color: black;">jspnpm</span> <span style="color: black;">create vite vite-demo</span>
<span style="color: black;">cd</span> <span style="color: black;">vite-demo</span>
<span style="color: black;">pnpm</span> <span style="color: black;">install</span>
<span style="color: black;">pnpm</span> <span style="color: black;">dev</span>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">editorconfig</h1>在项目根目录下新建一个 .editorconfig 文件,<span style="color: black;">重点</span><span style="color: black;">功效</span>为<span style="color: black;">掌控</span>编辑器<span style="color: black;">行径</span>(需要安装EditorConfig for VS Code插件)<span style="color: black;">root</span> = <span style="color: black;">true</span>
<span style="color: black;">
[*]</span> <span style="color: black;"># <span style="color: black;">暗示</span>所有文件适用</span>
<span style="color: black;">charset</span> = utf-<span style="color: black;">8</span> <span style="color: black;"># 设置文件字符集为 utf-8</span>
<span style="color: black;">indent_style</span> = space <span style="color: black;"># 缩进风格(tab | space)</span>
<span style="color: black;">indent_size</span> = <span style="color: black;">2</span> <span style="color: black;"># 缩进<span style="color: black;">体积</span></span>
<span style="color: black;">end_of_line</span> = lf <span style="color: black;"># <span style="color: black;">掌控</span>换行类型(lf | cr | crlf)</span>
<span style="color: black;">trim_trailing_whitespace</span> = <span style="color: black;">true</span> <span style="color: black;"># 去除行首的任意空白字符</span>
<span style="color: black;">insert_final_newline</span> = <span style="color: black;">true</span> <span style="color: black;"># 始终在文件末尾<span style="color: black;">插进</span>一个新行</span>
<span style="color: black;">
[*.md]</span> <span style="color: black;"># <span style="color: black;">暗示</span>仅 md 文件适用以下规则</span>
<span style="color: black;">max_line_length</span> = <span style="color: black;">off</span>
<span style="color: black;">trim_trailing_whitespace</span> = <span style="color: black;">false</span>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">prettier</h1>在项目根目录下新建一个 .prettierrc.js 文件,<span style="color: black;">重点</span><span style="color: black;">功效</span>为格式化代码pnpm i prettier -D<span style="color: black;">module.exports</span> <span style="color: black;">=</span> <span style="color: black;">{</span>
<span style="color: black;">singleQuote:</span> <span style="color: black;">true</span><span style="color: black;">,</span>
<span style="color: black;">printWidth:</span> <span style="color: black;">100</span><span style="color: black;">,</span>
<span style="color: black;">tabWidth:</span> <span style="color: black;">2</span><span style="color: black;">,</span>
<span style="color: black;">useTabs:</span> <span style="color: black;">false</span><span style="color: black;">,</span>
<span style="color: black;">semi:</span> <span style="color: black;">true</span><span style="color: black;">,</span>
<span style="color: black;">trailingComma:</span> <span style="color: black;">none</span><span style="color: black;">,</span>
<span style="color: black;">endOfLine:</span> <span style="color: black;">auto</span>
<span style="color: black;">}</span>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">ESLint</h1>在项目根目录下新建一个 .eslintrc.js 文件,<span style="color: black;">重点</span><span style="color: black;">功效</span>为修复代码格式错误(需要安装ESLint插件)pnpm i eslint eslint-config-prettier eslint-define-config eslint-plugin-prettier eslint-plugin-vue @typescript-eslint/eslint-plugin @typescript-eslint/parser @vue/eslint-config-typescript -D<span style="color: black;">module</span>.<span style="color: black;">exports</span> = {
root: <span style="color: black;">true</span>,
env: {
node: <span style="color: black;">true</span>
},
parser: <span style="color: black;">vue-eslint-parser</span>,
parserOptions: {
parser:<span style="color: black;">@typescript-eslint/parser</span>,
ecmaVersion: <span style="color: black;">2018</span>,
sourceType: <span style="color: black;">module</span>,
jsxPragma: <span style="color: black;">React</span>,
ecmaFeatures: {
jsx: <span style="color: black;">true</span>}
},
extends: [<span style="color: black;">plugin:vue/vue3-recommended</span>,
<span style="color: black;">plugin:@typescript-eslint/recommended</span>,
<span style="color: black;">plugin:prettier/recommended</span>,
<span style="color: black;">eslint-config-prettier</span>
],
rules: {
<span style="color: black;">space-before-function-paren</span>: <span style="color: black;">off</span>,
<span style="color: black;">@typescript-eslint/ban-ts-ignore</span>: <span style="color: black;">off</span>,
<span style="color: black;">@typescript-eslint/explicit-function-return-type</span>: <span style="color: black;">off</span>,
<span style="color: black;">@typescript-eslint/no-explicit-any</span>: <span style="color: black;">off</span>,
<span style="color: black;">@typescript-eslint/no-var-requires</span>: <span style="color: black;">off</span>,
<span style="color: black;">@typescript-eslint/no-empty-function</span>: <span style="color: black;">off</span>,
<span style="color: black;">@typescript-eslint/no-use-before-define</span>: <span style="color: black;">off</span>,
<span style="color: black;">@typescript-eslint/ban-ts-comment</span>: <span style="color: black;">off</span>,
<span style="color: black;">@typescript-eslint/ban-types</span>: <span style="color: black;">off</span>,
<span style="color: black;">@typescript-eslint/no-non-null-assertion</span>: <span style="color: black;">off</span>,
<span style="color: black;">@typescript-eslint/explicit-module-boundary-types</span>: <span style="color: black;">off</span>,
<span style="color: black;">@typescript-eslint/no-unused-vars</span>: [
<span style="color: black;">error</span>,
{
argsIgnorePattern: <span style="color: black;">^_</span>,
varsIgnorePattern: <span style="color: black;">^_</span>
}
],
<span style="color: black;">no-use-before-define</span>: <span style="color: black;">off</span>,
<span style="color: black;">no-unused-vars</span>: [
<span style="color: black;">error</span>,
{
argsIgnorePattern:<span style="color: black;">^_</span>,
varsIgnorePattern: <span style="color: black;">^_</span>
}
],
<span style="color: black;">vue/comment-directive</span>: <span style="color: black;">off</span>,
<span style="color: black;">vue/script-setup-uses-vars</span>: <span style="color: black;">error</span>,
<span style="color: black;">vue/multi-word-component-names</span>: <span style="color: black;">off</span>,
<span style="color: black;">vue/custom-event-name-casing</span>: <span style="color: black;">off</span>,
<span style="color: black;">vue/attributes-order</span>: <span style="color: black;">off</span>,
<span style="color: black;">vue/one-component-per-file</span>: <span style="color: black;">off</span>,
<span style="color: black;">vue/html-closing-bracket-newline</span>: <span style="color: black;">off</span>,
<span style="color: black;">vue/max-attributes-per-line</span>: <span style="color: black;">off</span>,
<span style="color: black;">vue/multiline-html-element-content-newline</span>: <span style="color: black;">off</span>,
<span style="color: black;">vue/singleline-html-element-content-newline</span>: <span style="color: black;">off</span>,
<span style="color: black;">vue/attribute-hyphenation</span>: <span style="color: black;">off</span>,
<span style="color: black;">vue/require-default-prop</span>: <span style="color: black;">off</span>,
<span style="color: black;">vue/html-self-closing</span>: [
<span style="color: black;">error</span>,
{
html: {
<span style="color: black;">void</span>: <span style="color: black;">always</span>,
normal: <span style="color: black;">never</span>,
component: <span style="color: black;">always</span>
},
svg: <span style="color: black;">always</span>,
math: <span style="color: black;">always</span>}
]
}
}<h1 style="color: black; text-align: left; margin-bottom: 10px;">ts</h1>在项目根目录下新建一个 tsconfig.json 文件{
<span style="color: black;">"compilerOptions"</span>: {
<span style="color: black;">"baseUrl"</span>: <span style="color: black;">"."</span>,
<span style="color: black;">"module"</span>: <span style="color: black;">"ESNext"</span>,
<span style="color: black;">"target"</span>: <span style="color: black;">"ESNext"</span>,
<span style="color: black;">"lib"</span>: [<span style="color: black;">"DOM"</span>, <span style="color: black;">"ESNext"</span>],
<span style="color: black;">"strict"</span>:<span style="color: black;">true</span>,
<span style="color: black;">"esModuleInterop"</span>: <span style="color: black;">true</span>,
<span style="color: black;">"allowSyntheticDefaultImports"</span>: <span style="color: black;">true</span>,
<span style="color: black;">"jsx"</span>: <span style="color: black;">"preserve"</span>,
<span style="color: black;">"moduleResolution"</span>: <span style="color: black;">"node"</span>,
<span style="color: black;">"isolatedModules"</span>: <span style="color: black;">true</span>,
<span style="color: black;">"resolveJsonModule"</span>:<span style="color: black;">true</span>,
<span style="color: black;">"noUnusedLocals"</span>: <span style="color: black;">true</span>,
<span style="color: black;">"strictNullChecks"</span>: <span style="color: black;">true</span>,
<span style="color: black;">"forceConsistentCasingInFileNames"</span>: <span style="color: black;">true</span>,
<span style="color: black;">"paths"</span>: {
<span style="color: black;">"@/*"</span>: [<span style="color: black;">"./src/*"</span>]
},
<span style="color: black;">"types"</span>: [<span style="color: black;">"vite/client"</span>,<span style="color: black;">"node"</span>]
},
<span style="color: black;">"include"</span>: [
<span style="color: black;">"src/**/*.ts"</span>,
<span style="color: black;">"src/**/*.d.ts"</span>,
<span style="color: black;">"src/**/*.tsx"</span>,
<span style="color: black;">"src/**/*.vue"</span>,
<span style="color: black;">"types/**/*.d.ts"</span>,
<span style="color: black;">"types/**/*.ts"</span>,
<span style="color: black;">"build/**/*.ts"</span>,
<span style="color: black;">"build/**/*.d.ts"</span>,
<span style="color: black;">"mock/**/*.ts"</span>,
<span style="color: black;">"vite.config.ts"</span>
],
<span style="color: black;">"exclude"</span>: [<span style="color: black;">"node_modules"</span>, <span style="color: black;">"dist"</span>]
}
<h1 style="color: black; text-align: left; margin-bottom: 10px;">husky</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">重点</span><span style="color: black;">功效</span>在于提交代码前为<span style="color: black;">咱们</span>做<span style="color: black;">有些</span>事情,如格式化、<span style="color: black;">检测</span>提交规范等(需要先初始化git)</p>1.pnpm install -D husky2.npx husky-init 在.husky/pre-commit下加入下面的代码<span style="color: black;">#!/usr/bin/env sh</span>
. <span style="color: black;">"<span style="color: black;">$(dirname -- "$0")</span>/_/husky.sh"</span>
// 对应的script脚本为<span style="color: black;">"lint"</span>: <span style="color: black;">"eslint \"{src,mock,build}/**/*.{vue,ts,tsx}\" --fix"</span>
pnpm lint
<h1 style="color: black; text-align: left; margin-bottom: 10px;">commitlint</h1><span style="color: black;">重点</span><span style="color: black;">功效</span>为提交代码时的规范名<span style="color: black;">叫作</span><span style="color: black;">检测</span>在项目根目录下新建一个 commitlint.config.js 文件pnpm install -D @commitlint/config-conventional @commitlint/clinpx husky add .husky/commit-msg npx --no -- commitlint --edit ${1}<span style="color: black;">module</span>.<span style="color: black;">exports</span> = {
extends: [<span style="color: black;">@commitlint/config-conventional</span>],
rules: {
<span style="color: black;">body-leading-blank</span>: [<span style="color: black;">2</span>, <span style="color: black;">always</span>],
<span style="color: black;">footer-leading-blank</span>: [<span style="color: black;">1</span>, <span style="color: black;">always</span>],
<span style="color: black;">header-max-length</span>: [<span style="color: black;">2</span>, <span style="color: black;">always</span>, <span style="color: black;">108</span>],
<span style="color: black;">subject-empty</span>: [<span style="color: black;">2</span>, <span style="color: black;">never</span>],
<span style="color: black;">type-empty</span>: [<span style="color: black;">2</span>, <span style="color: black;">never</span>],
<span style="color: black;">subject-case</span>: [<span style="color: black;">0</span>],
<span style="color: black;">type-enum</span>: [
<span style="color: black;">2</span>,
<span style="color: black;">always</span>,
[
<span style="color: black;">feat</span>, <span style="color: black;">// 新功能</span>
<span style="color: black;">fix</span>, <span style="color: black;">// 修复bug</span>
<span style="color: black;">perf</span>, <span style="color: black;">// 性能优化</span>
<span style="color: black;">style</span>, <span style="color: black;">// 修改代码格式,不影响代码<span style="color: black;">规律</span></span>
<span style="color: black;">docs</span>, <span style="color: black;">// 修改文档</span>
<span style="color: black;">test</span>, <span style="color: black;">// 修改测试用例</span>
<span style="color: black;">refactor</span>, <span style="color: black;">// 代码重构</span>
<span style="color: black;">build</span>, <span style="color: black;">// 构建流程、<span style="color: black;">外边</span>依赖变更(如升级 npm 包、修改 webpack 配置等)</span>
<span style="color: black;">ci</span>, <span style="color: black;">// <span style="color: black;">连续</span>集成</span>
<span style="color: black;">chore</span>, <span style="color: black;">// 其它提交</span>
<span style="color: black;">revert</span>, <span style="color: black;">// 代码回滚</span>
<span style="color: black;">types</span>, <span style="color: black;">// ts</span>
<span style="color: black;">wip</span> <span style="color: black;">// 功能<span style="color: black;">研发</span>中</span>
]
]
}
}
<h1 style="color: black; text-align: left; margin-bottom: 10px;">约束包管理器</h1>在package.json里新增scripts脚本"preinstall": "npx only-allow pnpm",<span style="color: black;">功效</span>项目里只能<span style="color: black;">运用</span>pnpm<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">好了,初期的<span style="color: black;">有些</span>工程化<span style="color: black;">咱们</span>就做好了,后面的后面再说吧~</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">作者:小心海一拳打爆提瓦特</p><span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">链接:</p>https://juejin.cn/post/7281185552811917375
</span>
软文发布平台 http://www.fok120.com/
页:
[1]