介绍
vue-pure-admin (opens new window)是一款开源免费且开箱即用的中后台管理系统模版。使用 Vue3
、Vite
、Element-Plus
、TypeScript
、Pinia
、Tailwindcss
等主流技术栈开发
#预览
开发环境
node
版本应不小于 16
,pnpm
版本应不小于 6
如果您还没安装 pnpm
,请执行下面命令进行安装(mac
用户遇到安装报错请在命令前加上 sudo
)
npm install -g pnpm
如果您需要安装多个 node
版本环境,请参考 Node.js 版本管理工具
如果您觉得安装平台依赖慢,请参考 npmmirror
#IDE
如果您使用的 IDE
是 vscode (opens new window)(推荐),请点击这里并安装推荐的插件,可提高开发效率
#拉取代码
#从 GitHub
上拉取
完整版前端代码
git clone https://github.com/pure-admin/vue-pure-admin.git
国际化精简版前端代码
git clone -b i18n https://github.com/pure-admin/pure-admin-thin.git
非国际化精简版前端代码
git clone https://github.com/pure-admin/pure-admin-thin.git
后端代码(node
版本)
git clone https://github.com/pure-admin/pure-admin-backend.git
#从 Gitee
上拉取
完整版前端代码
git clone https://gitee.com/yiming_chang/vue-pure-admin.git
国际化精简版前端代码
git clone -b i18n https://gitee.com/yiming_chang/pure-admin-thin.git
非国际化精简版前端代码
git clone https://gitee.com/yiming_chang/pure-admin-thin.git
后端代码(node
版本)
git clone https://gitee.com/yiming_chang/pure-admin-backend.git
#本地开发
安装依赖
pnpm install
启动平台
pnpm dev
安装一个包
pnpm add 包名
卸载一个包
pnpm remove 包名
完整版目录结构
├── .github # GitHub 配置文件
│ ├── ISSUE_TEMPLATE # 问题提交参考模板
│ ├── workflows git # 工作流
├── .husky # 代码提交前校验配置文件
├── .vscode # IDE 工具推荐配置文件
│ │ ├── extensions.json # 一键安装平台推荐的 vscode 插件
│ │ ├── settings.json # 设置扩展程序或 vscode 编辑器的一些属性
│ │ ├── vue3.0.code-snippets # vue3.0 代码片段
│ │ └── vue3.2.code-snippets # vue3.2 代码片段
├── build # 构建工具
│ │ ├── cdn.ts # 打包时采用 cdn 模式
│ │ ├── compress.ts # 打包时启用 gzip 压缩或 brotli 压缩
│ │ ├── index.ts # 导出环境变量、跨域代理函数
│ │ ├── info.ts # 输出打包信息(大小、用时)
│ │ ├── optimize.ts # vite 依赖预构建配置项
│ │ ├── plugins.ts # vite 相关插件存放处
├── locales # 国际化文件存放处
│ │ ├── en.yaml # 英文配置
│ │ ├── zh-CN.yaml # 中文配置
├── mock # mock 模拟后台数据
│ │ ├── asyncRoutes.ts # 模拟后台返回动态路由
│ │ ├── ...
├── node_modules # 模块依赖
├── public # 静态资源
│ │ ├── html # 静态 iframe 页面
│ │ ├── favicon.ico # favicon
│ │ ├── serverConfig.json # 全局配置文件(打包后修改也可生效)
├── src
│ ├── api # 接口请求统一管理
│ ├── assets # 字体、图片等静态资源
│ ├── components # 自定义通用组件
│ │ ├── ReAuth # 按钮级别权限管理组件
│ │ ├── ReBarcode # 条形码组件
│ │ ├── ReCountTo # 数字动画组件
│ │ ├── ReCropper # 图片裁剪组件
│ │ ├── ReFlicker # 圆点、方形闪烁动画组件
│ │ ├── ReFlop # 时间翻牌组件
│ │ ├── ReFlowChart # LogicFlow 流程图组件
│ │ ├── ReIcon # 图标组件
│ │ ├── ReImageVerify # 图形验证码组件
│ │ ├── ReMap # 高德地图组件
│ │ ├── ReQrcode # 二维码组件
│ │ ├── ReSeamlessScroll # 无缝滚动组件
│ │ ├── ReSelector # 选择器组件
│ │ ├── ReSplitPane # 切割面板组件
│ │ ├── ReTreeLine # 树形连接线组件(基于element-plus)
│ │ ├── ReTypeit # 打字机效果组件
│ ├── config # 获取平台动态全局配置
│ ├── directives # 自定义指令
│ │ ├── auth # 按钮级别权限指令
│ │ ├── elResizeDetector # 监听容器改变指令
│ ├── layout # 主要页面布局
│ ├── plugins # 处理一些库或插件,导出更方便的 api
│ ├── router # 路由配置
│ ├── store # pinia 状态管理
│ ├── style # 全局样式
│ │ ├── dark.scss # 暗黑模式样式适配文件
│ │ ├── element-plus.scss # 全局覆盖 element-plus 样式文件
│ │ ├── reset.scss # 全局重置样式文件
│ │ ├── sidebar.scss # layout 布局样式文件
│ │ ├── tailwind.css # tailwindcss 自定义样式配置文件
│ │ ├── ...
│ ├── utils # 全局工具方法
│ │ ├── http # 封装 axios 文件
│ │ ├── progress # 封装 nprogress
│ │ └── auth.ts # 处理用户信息和 token 相关
│ │ └── chinaArea.ts # 汉字转区域码
│ │ └── globalPolyfills.ts # 解决项目可能因为安装某个依赖出现 `global is not defined` 报错
│ │ └── message.ts # 消息提示函数
│ │ ├── mitt.ts # 触发公共事件,类似 EventBus
│ │ ├── print.ts # 打印函数
│ │ ├── propTypes.ts # 二次封装 vue 的 propTypes
│ │ ├── responsive.ts # 全局响应式 storage 配置
│ │ ├── sso.ts # 前端单点登录逻辑处理
│ │ ├── tree.ts # 树结构相关处理函数
│ ├── views # 存放编写业务代码页面
│ ├── App.vue # 入口页面
│ ├── main.ts # 入口文件
│ └── mockProdServer.ts # mock 服务相关
├── types # 全局 TS 类型配置
│ │ ├── global.d.ts # 全局类型声明文件
│ │ ├── index.d.ts # 全局类型声明文件
│ │ ├── index.ts # 全局类型声明文件
│ │ ├── shims-tsx.d.ts # 该文件是为了给 .tsx 文件提供类型支持,在编写时能正确识别语法
│ │ └── shims-vue.d.ts # .vue、.scss 文件不是常规的文件类型,typescript 无法识别,所以我们需要通过下图的代码告诉 typescript 这些文件的类型,防止类型报错
├── .editorconfig # 编辑器读取文件格式及样式定义配置 https://editorconfig.org/
├── .env # 全局环境变量配置(当 .env 文件与 .env.development、.env.production、.env.staging 这三个文件之一存在相同的配置 key 时,.env 优先级更低)
├── .env.development # 开发环境变量配置
├── .env.production # 生产环境变量配置
├── .env.staging # 预发布环境变量配置
├── .eslintignore # eslint 语法检查忽略文件
├── .eslintrc.js # eslint 语法检查配置
├── .gitignore # git 提交忽略文件
├── .gitpod.yml # gitpod 部署配置
├── .markdownlint.json # markdown 格式检查配置
├── .npmrc # npm 配置文件
├── .prettierrc.js # prettier 插件配置
├── .stylelintignore # stylelint 插件检查忽略文件
├── CHANGELOG.en_US.md # 版本更新日志(英文版)
├── CHANGELOG.md # 版本更新日志(英文版)
├── CHANGELOG.zh_CN.md # 版本更新日志(中文版)
├── commitlint.config.js # git 提交前检查配置
├── index.html # html 主入口
├── LICENSE # 证书
├── package.json # 依赖包管理以及命令配置
├── pnpm-lock.yaml # 依赖包版本锁定文件
├── postcss.config.js # postcss 插件配置
├── README.en-US.md # README(英文版)
├── README.md # README
├── stylelint.config.js # stylelint 配置
├── tailwind.config.js # tailwindcss 配置
├── tsconfig.json # typescript 配置
└── vite.config.ts # vite 配置
环境变量
#配置文件
一共 四
个配置文件,都在平台根目录下,具体如下
├── .env # 基础环境变量配置文件(优先级最低)
├── .env.development # 开发环境变量配置文件
├── .env.production # 生产环境变量配置文件
├── .env.staging # 预发布环境变量配置文件
#具体配置
平台内置 五
个环境变量,具体如下
# 平台本地运行端口号
VITE_PORT = 8848
# 平台打包路径
VITE_PUBLIC_PATH = /
# 平台路由历史模式(Hash模式传"hash"、HTML5模式传"h5"、Hash模式带base参数传"hash,base参数"、HTML5模式带base参数传"h5,base参数")
VITE_ROUTER_HISTORY = "hash"
# 是否在打包时使用cdn替换本地库 替换 true 不替换 false
VITE_CDN = false
# 是否启用gzip压缩或brotli压缩(分两种情况,删除原始文件和不删除原始文件)
# 压缩时不删除原始文件的配置:gzip、brotli、both(同时开启 gzip 与 brotli 压缩)、none(不开启压缩,默认)
# 压缩时删除原始文件的配置:gzip-clear、brotli-clear、both-clear(同时开启 gzip 与 brotli 压缩)、none(不开启压缩,默认)
VITE_COMPRESSION = "none"
#基础用法
.env 文件 (opens new window)vite文档
const { VITE_PUBLIC_PATH } = import.meta.env;
console.log("当前环境变量VITE_PUBLIC_PATH为:", VITE_PUBLIC_PATH)
知识点:如何在 .vue 或 .ts 文件中判断当前是开发环境还是生产环境?
打印 process.env.NODE_ENV
,值为 development
代表是开发环境,值为 production
代表是生产环境
#如何添加自定义配置
比如要在 .env.production
文件添加自定义配置 VITE_HANDSOME = true
,作为一种规范推荐使用 VITE_
作为自定义配置前缀
① 在 .env.production
中加入 VITE_HANDSOME = true
,具体如下
# 自定义配置
VITE_HANDSOME = true
② 加入类型支持,在 interface ViteEnv (opens new window)里加上 VITE_HANDSOME: boolean
,具体如下
interface ViteEnv {
VITE_HANDSOME: boolean;
}
③ 设置默认值,在 build/index.ts (opens new window)文件的 ret
变量上加上 VITE_HANDSOME: true
,具体如下
const ret: ViteEnv = {
VITE_HANDSOME: true
}
#serverConfig.json
serverConfig.json (opens new window)配置文件在任何环境下修改都可生效
{
"Version": "3.9.6", // 平台版本号
"Title": "PureAdmin", // 平台标题
"FixedHeader": true, // 是否固定页头和标签页(true 内容区超出出现纵向滚动条 false 页头、标签页、内容区可纵向滚动)
"HiddenSideBar": false, // 隐藏菜单和页头,只显示标签页和内容区
"MultiTagsCache": false, // 是否开启持久化标签 (会缓存)
"KeepAlive": true, // 是否开启组件缓存(此处不同于路由的 keepAlive,如果此处为 true 表示设置路由的 keepAlive 起效,反之设置 false 屏蔽平台整体的 keepAlive,即使路由设置了keepAlive 也不再起作用)
"Locale": "zh", // 默认国际化语言 (zh 中文、en 英文)(会缓存)
"Layout": "vertical", // 导航菜单模式 (vertical 左侧菜单模式、horizontal 顶部菜单模式、mix 综合菜单模式)(会缓存)
"Theme": "default", // 主题模式(会缓存)
"DarkMode": false, // 是否开启暗黑模式 (会缓存)
"Grey": false, // 灰色模式(会缓存)
"Weak": false, // 色弱模式(会缓存)
"HideTabs": false, // 是否隐藏标签页(会缓存)
"SidebarStatus": true, // vertical左侧菜单模式模式下侧边栏状态(true 展开、false 收起)(会缓存)
"EpThemeColor": "#409EFF", // 主题色(会缓存)
"ShowLogo": true, // 是否显示logo(会缓存)
"ShowModel": "smart", // 标签页风格(smart 灵动模式、card 卡片模式)(会缓存)
"MenuArrowIconNoTransition": false, // 菜单展开、收起图标是否开启动画,如遇菜单展开、收起卡顿设置成 true 即可(默认 false,开启动画)
"CachingAsyncRoutes": true, // 是否开启动态路由缓存本地的全局配置,默认 true
"TooltipEffect": "light" // 可配置平台主体所有 el-tooltip 的 effect 属性,默认 light,不会影响业务代码
}
提示
开发、生产环境修改都可生效,上面 serverConfig.json
文件标记 会缓存
即配置会缓存在本地浏览器,应先清空浏览器缓存才能生效
#基础用法
平台将 serverConfig.json
中所有配置都已经注册到 $config (opens new window)这个全局属性对象上,应用内所有组件实例都可访问 globalproperties(opens new window)
#.vue
文件中使用(setup
语法)
<script setup lang="ts">
import { getCurrentInstance } from "vue";
const { Version } = getCurrentInstance().appContext.config.globalProperties.$config;
console.log("Version:", Version)
</script>
#.vue
文件中使用(非 setup
语法)
<script lang="ts">
export default defineComponent({
computed: {
currentConfig() {
return this.$config?.Version;
}
}
});
</script>
#.ts
或 .vue
中使用
具体实现代码位置 config/index.ts(opens new window)
import { getConfig } from "@/config";
console.log("Version:", getConfig()?.Version)
#如何添加自定义配置
比如要添加的自定义配置为 Handsome: true
① 在 serverConfig.json
中加入 Handsome: true
,具体如下
{
"Handsome": true // 自定义配置
}
② 加入类型支持,在 interface ServerConfigs (opens new window)里加上 Handsome?: boolean
,具体如下
interface ServerConfigs {
Handsome?: boolean; // 根据实际类型编写
}
#package.json
package.json (opens new window)文件中的 scripts
配置中存放平台的各种命令,具体如下
{
"scripts": {
"dev": "NODE_OPTIONS=--max-old-space-size=4096 vite", // 启动平台
"serve": "pnpm dev", // 启动平台(有人喜欢dev、有人喜欢serve)
"build": "rimraf dist && NODE_OPTIONS=--max-old-space-size=8192 vite build", // 打包平台(rimraf 包的作用:以包的形式包装rm -rf命令,用来删除文件和文件夹的,不管文件夹是否为空,都可删除)
"build:staging": "rimraf dist && vite build --mode staging", // 打包平台(预发布环境)
"report": "rimraf dist && vite build", // 打包平台并生产平台包文件大小图形化分析
"preview": "vite preview", // 预览平台,需先打包(无需安装live-server等工具,vite自带预览功能)
"preview:build": "pnpm build && vite preview", // 打包并预览平台(无需安装live-server等工具,vite自带预览功能)
"typecheck": "tsc --noEmit && vue-tsc --noEmit --skipLibCheck", // 使用vue-tsc工具对指定的.ts、.tsx、.vue文件进行类型校验
"svgo": "svgo -f src/assets/svg -o src/assets/svg", // 使用svgo工具对指定目录里的所有svg文件进行压缩
"cloc": "NODE_OPTIONS=--max-old-space-size=4096 cloc . --exclude-dir=node_modules --exclude-lang=YAML", // 平台文件、语言分析
"clean:cache": "rm -rf node_modules && rm -rf .eslintcache && pnpm install", // 删除node_modules、清空eslint缓存并重新安装平台依赖
"lint:eslint": "eslint --cache --max-warnings 0 \"{src,mock,build}/**/*.{vue,js,ts,tsx}\" --fix", // eslint修复
"lint:prettier": "prettier --write \"src/**/*.{js,ts,json,tsx,css,less,scss,vue,html,md}\"", // prettier格式化
"lint:stylelint": "stylelint --cache --fix \"**/*.{vue,css,scss,postcss,less}\" --cache --cache-location node_modules/.cache/stylelint/", // stylelint格式化修复
"lint:lint-staged": "lint-staged -c ./.husky/lintstagedrc.js", // lint-staged能够让lint只检测暂存区的文件(这里用于husky,提交前校验)
"lint:pretty": "pretty-quick --staged", // 自动运行prettier(这里用于husky,提交前校验)
"lint": "pnpm lint:eslint && pnpm lint:prettier && pnpm lint:stylelint", // 平台整体lint格式化并修复
"prepare": "husky install", // 自动生成husky(https://typicode.github.io/husky/#/)
"preinstall": "npx only-allow pnpm" // 只允许运行pnpm命令,如果您换成yarn、npm需要把这行删除(https://pnpm.io/zh/only-allow-pnpm)
}
}