目的:快速构建一个vue后台项目。
通过初始化和查询element组件,可以解决大部分问题,因为大部分都是表格的处理。

资料:

  • element UI 各种组件
  • vue element Admin 提供全面的功能,预览页面,包含:Element UI & axios & iconfont & permission control & lint等基础模块
  • vue-admin-template 提供基础功能,建议初始化使用

vue-admin-templat

新建项目

完成 动态路由导航、登录、axios等基础功能

  1. # 克隆项目
  2. git clone https://github.com/PanJiaChen/vue-admin-template.git
  3. # 进入项目目录
  4. cd vue-admin-template
  5. # 安装依赖
  6. npm install
  7. # 建议不要直接使用 cnpm 安装以来,会有各种诡异的 bug。可以通过如下操作解决 npm 下载速度慢的问题
  8. npm install --registry=https://registry.npm.taobao.org
  9. # 启动服务
  10. npm run dev

项目初始化

request.js

修改axios的过滤器,结果如下

  1. import axios from 'axios'
  2. import store from '@/store'
  3. import { getToken } from '@/utils/auth'
  4. // create an axios instance
  5. const service = axios.create({
  6. baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  7. // withCredentials: true, // send cookies when cross-domain requests
  8. timeout: 5000 // request timeout
  9. })
  10. // request interceptor
  11. service.interceptors.request.use(
  12. config => {
  13. // do something before request is sent
  14. if (store.getters.token) {
  15. // let each request carry token
  16. // ['X-Token'] is a custom headers key
  17. // please modify it according to the actual situation
  18. config.headers['X-Token'] = getToken()
  19. }
  20. return config
  21. },
  22. error => {
  23. // do something with request error
  24. console.log(error) // for debug
  25. return Promise.reject(error)
  26. }
  27. )
  28. // response interceptor
  29. service.interceptors.response.use(
  30. /**
  31. * If you want to get http information such as headers or status
  32. * Please return response => response
  33. */
  34. /**
  35. * Determine the request status by custom code
  36. * Here is just an example
  37. * You can also judge the status by HTTP Status Code
  38. */
  39. response => {
  40. const res = response.data
  41. return res
  42. },
  43. error => {
  44. console.log('err' + error) // for debug
  45. Message({
  46. message: error.message,
  47. type: 'error',
  48. duration: 5 * 1000
  49. })
  50. return Promise.reject(error)
  51. }
  52. )
  53. export default service

表格页面示例

效果:

image.png

新建api请求

  1. import request from '@/utils/request'
  2. export function fetchList(query) {
  3. return request({
  4. url: '/article/list',
  5. method: 'get',
  6. params: query
  7. })
  8. }
  9. export function fetchOne(id) {
  10. return request({
  11. url: '/article/detail',
  12. method: 'get',
  13. params: { id }
  14. })
  15. }
  16. // create Or update
  17. export function saveOne(data) {
  18. return request({
  19. url: '/article/save',
  20. method: 'post',
  21. data
  22. })
  23. }
  24. export function deleteOne(id) {
  25. return request({
  26. url: '/article/delete',
  27. method: 'post',
  28. params: { id }
  29. })
  30. }

新建vue页面

  1. <template>
  2. <div class="app-container">
  3. <el-form :inline="true" :model="param" class="demo-form-inline">
  4. <el-form-item label="名字">
  5. <el-input v-model="param.name" placeholder="名字" />
  6. </el-form-item>
  7. <el-form-item label="日期">
  8. <el-input v-model="param.date" />
  9. </el-form-item>
  10. <el-form-item>
  11. <el-button type="primary" @click="queryList">查询</el-button>
  12. </el-form-item>
  13. </el-form>
  14. <template>
  15. <!--添加-->
  16. <el-row>
  17. <el-button @click="createOne()">添加</el-button>
  18. </el-row>
  19. <!--数据-->
  20. <el-table
  21. :data="tableData"
  22. border
  23. style="width: 100%"
  24. >
  25. <el-table-column
  26. fixed
  27. prop="date"
  28. label="日期"
  29. width="150"
  30. />
  31. <el-table-column
  32. prop="name"
  33. label="姓名"
  34. width="150"
  35. />
  36. <el-table-column
  37. prop="email"
  38. label="email地址"
  39. width="300"
  40. />
  41. <el-table-column
  42. fixed="right"
  43. label="操作"
  44. width="100"
  45. >
  46. <template slot-scope="scope">
  47. <el-button
  48. type="text"
  49. size="small"
  50. @click="editOne(scope.row)"
  51. >编辑
  52. </el-button>
  53. <el-button
  54. size="small"
  55. type="text"
  56. @click="deleteAlert(scope.row.id)"
  57. >删除
  58. </el-button>
  59. </template>
  60. </el-table-column>
  61. </el-table>
  62. <!--编辑层-->
  63. <el-dialog title="编辑" :visible.sync="dialogFormVisible">
  64. <el-form :model="editNews">
  65. <el-form-item label="姓名" :label-width="formLabelWidth">
  66. <el-input v-model="editNews.name" autocomplete="off" />
  67. </el-form-item>
  68. <el-form-item label="Email" :label-width="formLabelWidth">
  69. <el-input v-model="editNews.email" placeholder="" />
  70. </el-form-item>
  71. </el-form>
  72. <div slot="footer" class="dialog-footer">
  73. <el-button @click="dialogFormVisible = false">取 消</el-button>
  74. <el-button type="primary" @click="saveOne">确 定</el-button>
  75. </div>
  76. </el-dialog>
  77. <!--删除提示-->
  78. <el-dialog
  79. title="提示"
  80. :visible.sync="dialogVisible"
  81. width="30%"
  82. :before-close="handleClose"
  83. >
  84. <span>真的要删吗?</span>
  85. <div slot="footer" class="dialog-footer">
  86. <el-button @click="dialogVisible = false">取 消</el-button>
  87. <el-button type="primary" @click="deleteOne">确 定</el-button>
  88. </div>
  89. </el-dialog>
  90. <!--分页-->
  91. <div class="block">
  92. <el-pagination
  93. :current-page="param.page"
  94. :page-sizes="[10, 20, 30, 40]"
  95. :page-size="param.size"
  96. layout="total, sizes, prev, pager, next, jumper"
  97. :total="total"
  98. @size-change="handleSizeChange"
  99. @current-change="handleCurrentChange"
  100. />
  101. </div>
  102. </template>
  103. </div>
  104. </template>
  105. <script>
  106. import { deleteOne, fetchList, fetchOne, saveOne } from '@/api/article'
  107. export default {
  108. data() {
  109. return {
  110. tableData: [],
  111. // 删除层隐藏
  112. dialogVisible: false,
  113. // 弹出层隐藏
  114. dialogFormVisible: false,
  115. // 弹出层内容
  116. editNews: {
  117. name: '',
  118. email: ''
  119. },
  120. // 弹出层大小
  121. formLabelWidth: '120px',
  122. // 删除使用的id
  123. id: '',
  124. // 查询参数
  125. param: {
  126. name: '',
  127. date: '',
  128. page: 1,
  129. size: 10
  130. },
  131. // 查询返回总条数
  132. total: 0
  133. }
  134. },
  135. mounted() {
  136. this.queryList()
  137. },
  138. methods: {
  139. // 修改页面展示数据大小
  140. handleSizeChange(val) {
  141. this.param.size = val
  142. this.queryList()
  143. },
  144. // 修改页码
  145. handleCurrentChange(val) {
  146. this.param.page = val
  147. this.queryList()
  148. },
  149. // 点击编辑,从后台获取数据
  150. editOne(row) {
  151. console.log(row)
  152. fetchOne(row.id).then(res => {
  153. if (res.flag) {
  154. this.editNews = res.data
  155. }
  156. })
  157. this.dialogFormVisible = true
  158. },
  159. // 获取删除行的id
  160. deleteAlert(id) {
  161. this.id = id
  162. this.dialogVisible = true
  163. },
  164. // 删除
  165. deleteOne() {
  166. deleteOne(this.id).then(res => {
  167. if (res.flag) {
  168. this.dialogFormVisible = false
  169. this.queryList()
  170. } else {
  171. alert('删除失败')
  172. }
  173. })
  174. this.dialogVisible = false
  175. },
  176. // 关闭提示框时,提示是否要关闭
  177. handleClose(done) {
  178. this.$confirm('确认关闭?')
  179. .then(() => {
  180. done()
  181. })
  182. .catch(() => {
  183. })
  184. },
  185. // 点击添加按钮清空
  186. createOne() {
  187. this.editNews = {}
  188. this.dialogFormVisible = true
  189. },
  190. // 点击确定后发送请求
  191. saveOne() {
  192. saveOne(this.editNews).then(res => {
  193. if (res.flag) {
  194. this.dialogFormVisible = false
  195. this.queryList()
  196. } else {
  197. this.$message('添加失败')
  198. }
  199. })
  200. },
  201. // 获取数据
  202. queryList() {
  203. fetchList(this.param).then(res => {
  204. if (res.flag) {
  205. this.tableData = res.data.list
  206. this.total = res.data.total
  207. } else {
  208. this.tableData = {}
  209. }
  210. })
  211. }
  212. }
  213. }
  214. </script>

mock模拟数据

  1. import Mock from 'mockjs'
  2. export default [
  3. {
  4. url: '/article/list',
  5. type: 'get',
  6. response: config => {
  7. console.log(config.query)
  8. console.log(config.body)
  9. const data = Mock.mock({
  10. code: 20000,
  11. flag: true,
  12. data: {
  13. 'total|100-200': 1,
  14. 'list|10': [{
  15. 'id|+1': 1,
  16. name: '@name',
  17. email: '@email',
  18. date: '@date'
  19. }]
  20. }
  21. })
  22. return data
  23. }
  24. },
  25. {
  26. url: '/article/detail',
  27. type: 'get',
  28. response: config => {
  29. const detail = Mock.mock({
  30. code: 20000,
  31. flag: true,
  32. data: {
  33. 'id|1-19': 1,
  34. name: '@name',
  35. email: '@email'
  36. }
  37. })
  38. return detail
  39. }
  40. },
  41. {
  42. url: '/article/save',
  43. type: 'post',
  44. response: x => {
  45. console.log(x.body)
  46. return {
  47. code: 20000,
  48. flag: true,
  49. data: 'success'
  50. }
  51. }
  52. },
  53. {
  54. url: '/article/deleteOne',
  55. type: 'post',
  56. response: _ => {
  57. return {
  58. code: 20000,
  59. flag: true,
  60. data: 'success'
  61. }
  62. }
  63. }
  64. ]

vue-element项目

项目包下载:vue-element-admin-master.zip

也可以:git clone https://github.com/PanJiaChen/vue-element-admin.git

所有的功能模块,需要的时候方便查找

  1. - 登录 / 注销
  2. - 权限验证
  3. - 页面权限
  4. - 指令权限
  5. - 权限配置
  6. - 二步登录
  7. - 多环境发布
  8. - dev sit stage prod
  9. - 全局功能
  10. - 国际化多语言
  11. - 多种动态换肤
  12. - 动态侧边栏(支持多级路由嵌套)
  13. - 动态面包屑
  14. - 快捷导航(标签页)
  15. - Svg Sprite 图标
  16. - 本地/后端 mock 数据
  17. - Screenfull全屏
  18. - 自适应收缩侧边栏
  19. - 编辑器
  20. - 富文本
  21. - Markdown
  22. - JSON 等多格式
  23. - Excel
  24. - 导出excel
  25. - 导入excel
  26. - 前端可视化excel
  27. - 导出zip
  28. - 表格
  29. - 动态表格
  30. - 拖拽表格
  31. - 内联编辑
  32. - 错误页面
  33. - 401
  34. - 404
  35. - 組件
  36. - 头像上传
  37. - 返回顶部
  38. - 拖拽Dialog
  39. - 拖拽Select
  40. - 拖拽看板
  41. - 列表拖拽
  42. - SplitPane
  43. - Dropzone
  44. - Sticky
  45. - CountTo
  46. - 综合实例
  47. - 错误日志
  48. - Dashboard
  49. - 引导页
  50. - ECharts 图表
  51. - Clipboard(剪贴复制)
  52. - Markdown2html