后台数据管理体系 - 项目架构设计-Vue3+axios+Element-plus(0920) ...

打印 上一主题 下一主题

主题 1008|帖子 1008|积分 3024

十三、文章分类页面 - [element-plus 表格]

Git堆栈:https://gitee.com/msyycn/vue3-hei-ma.git
基本架子 - PageContainer

功能需求说明:
   

  • 基本架子-PageContainer封装
  • 文章分类渲染 & loading处理
  • 文章分类添加编辑[element-plus弹层]
  • 文章分类删除
  

  • 基本结构样式,用到了el-card 组件
  1. <template>
  2.   <el-card class="page-container">
  3.     <template #header>
  4.       <div class="header">
  5.         <span>文章分类</span>
  6.         <div class="extra">
  7.           <el-button type="primary">添加分类</el-button>
  8.         </div>
  9.       </div>
  10.     </template>
  11.      ...
  12.   </el-card>
  13. </template>
  14. <style lang="scss" scoped>
  15. .page-container {
  16.   min-height: 100%;
  17.   box-sizing: border-box;
  18.   .header {
  19.     display: flex;
  20.     align-items: center;
  21.     justify-content: space-between;
  22.   }
  23. }
  24. </style>
复制代码

  • 考虑到多个页面复用,封装成组件

    • props 定制标题(父传子)
    • 默认插槽 default 定制内容主体
    • 具名插槽 extra 定制头部右侧额外的按钮

  1. <script setup>
  2. defineProps({
  3.   title: {
  4.     required: true,
  5.     type: String
  6.   }
  7. })
  8. </script>
  9. <template>
  10.   <el-card class="page-container">
  11.     <template #header>
  12.       <div class="header">
  13.         <span>{{ title }}</span>
  14.         <div class="extra">
  15.           <slot name="extra"></slot>
  16.         </div>
  17.       </div>
  18.     </template>
  19.     <slot></slot>
  20.   </el-card>
  21. </template>
  22. <style lang="scss" scoped>
  23. .page-container {
  24.   min-height: 100%;
  25.   box-sizing: border-box;
  26.   .header {
  27.     display: flex;
  28.     align-items: center;
  29.     justify-content: space-between;
  30.   }
  31. }
  32. </style>
复制代码

  • 页面中直接使用测试 ( unplugin-vue-components 会自动注册)


  • 文章分类测试:
  1. <template>
  2.   <page-container title="文章分类">
  3.     <template #extra>
  4.       <el-button type="primary"> 添加分类 </el-button>
  5.     </template>
  6.     主体部分
  7.   </page-container>
  8. </template>
复制代码


  • 文章管理测试:
  1. <template>
  2.   <page-container title="文章管理">
  3.     <template #extra>
  4.       <el-button type="primary">发布文章</el-button>
  5.     </template>
  6.     主体部分
  7.   </page-container>
  8. </template>
复制代码
视图预览

文章分类渲染

封装API - 哀求获取表格数据


  • 新建 api/article.js 封装获取频道列表的接口
  1. import request from '@/utils/request'
  2. export const artGetChannelsService = () => request.get('/my/cate/list')
复制代码

  • 页面中调用接口,获取数据存储
  1. const channelList = ref([])
  2. const getChannelList = async () => {
  3.   const res = await artGetChannelsService()
  4.   channelList.value = res.data.data
  5. }
复制代码

  • ArticleChannel.vue
  1. <script setup>
  2. import { artGetChannelsService } from '@/api/article'
  3. import {ref} from 'vue'
  4. const channelList = ref([]) // 文章分类列表
  5. // 获取文章分类列表
  6. const getChannelList = async () => {
  7.   const res = await artGetChannelsService()
  8.   channelList.value = res.data.data
  9.   console.log('文章分类列表',channelList.value);
  10.   
  11. }
  12. // 调用获取文章分类列表
  13. getChannelList()
  14. </script>
  15. <template>
  16.   <div>
  17.     <!-- 按需引入 -->
  18.     <page-container title="文章分类">
  19.     <!-- 右侧按钮 - 添加文章 - 具名插槽 -->
  20.       <template #extra>
  21.         <el-button>添加分类</el-button>
  22.       </template>
  23.       主体部分--表格
  24.     </page-container>
  25.    
  26.   </div>
  27. </template>
  28. <style lang="scss" scoped>
  29. </style>
复制代码
视图预览

el-table 表格动态渲染

  1. <el-table :data="channelList" style="width: 100%">
  2.   <el-table-column label="序号" width="100" type="index"> </el-table-column>
  3.   <el-table-column label="分类名称" prop="cate_name"></el-table-column>
  4.   <el-table-column label="分类别名" prop="cate_alias"></el-table-column>
  5.   <el-table-column label="操作" width="100">
  6.     <template #default="{ row }">
  7.       <el-button
  8.         :icon="Edit"
  9.         circle
  10.         plain
  11.         type="primary"
  12.         @click="onEditChannel(row)"
  13.       ></el-button>
  14.       <el-button
  15.         :icon="Delete"
  16.         circle
  17.         plain
  18.         type="danger"
  19.         @click="onDelChannel(row)"
  20.       ></el-button>
  21.     </template>
  22.   </el-table-column>
  23.   <template #empty>
  24.     <el-empty description="没有数据" />
  25.   </template>
  26. </el-table>
  27. const onEditChannel = (row) => {
  28.   console.log(row)
  29. }
  30. const onDelChannel = (row) => {
  31.   console.log(row)
  32. }
复制代码
预览视图

el-table 表格 loading 效果


  • 定义变量,v-loading绑定
  1. const loading = ref(false)
  2. <el-table v-loading="loading">
复制代码

  • 发送哀求前开启,哀求竣事关闭
  1. const getChannelList = async () => {
  2.   loading.value = true
  3.   const res = await artGetChannelsService()
  4.   channelList.value = res.data.data
  5.   loading.value = false
  6. }
复制代码
ArticleChannel.vue
  1. <script setup>
  2. import { artGetChannelsService } from '@/api/article'
  3. import { Delete,Edit } from   '@element-plus/icons-vue'
  4. import {ref} from 'vue'
  5. const channelList = ref([]) // 文章分类列表
  6. const loading = ref(false) //加载状态
  7. // 获取文章分类列表
  8. const getChannelList = async () => {
  9.   // 发请求之前先将loading设置为true
  10.   loading.value = true
  11.   // 调用接口
  12.   const res = await artGetChannelsService()
  13.   channelList.value = res.data.data
  14.   // 发完请求,关闭loading
  15.   loading.value = false
  16.   // console.log('文章分类列表',channelList.value);
  17.   
  18. }
  19. // 调用获取文章分类列表
  20. getChannelList()
  21. // 编辑文章分类
  22. const onEditChannel =(row,$index) =>{
  23. console.log(row,$index)
  24. }
  25. // 删除文章分类
  26. const onDelChannel =(row,$index)=>{
  27.   console.log(row,$index)
  28. }
  29. </script>
  30. <template>
  31.   <div>
  32.     <!-- 按需引入 -->
  33.     <page-container title="文章分类">
  34.     <!-- 右侧按钮 - 添加文章 - 具名插槽 -->
  35.       <template #extra>
  36.         <el-button>添加分类</el-button>
  37.       </template>
  38.       <!-- 主体部分--表格 -->
  39.       <el-table :data="channelList" style="width: 100%" v-loading="loading">
  40.           <el-table-column   label="序号"    type="index"  width="100" ></el-table-column>
  41.           <el-table-column   label="分类名称" prop="cate_name" ></el-table-column>
  42.           <el-table-column   label="分类别名" prop="cate_alias" ></el-table-column>
  43.           <el-table-column   label="操作"      width="100">
  44.           <template #default="{row,$index}">
  45.              <el-button  @click="onEditChannel(row,$index)" plain :icon="Edit" circle type="primary" ></el-button>
  46.              <el-button  @click="onDelChannel(row,$index)" plain :icon="Delete" circle type="danger" ></el-button>
  47.           </template>
  48.           </el-table-column>
  49.       </el-table>
  50.     </page-container>
  51.    
  52.   </div>
  53. </template>
  54. <style lang="scss" scoped>
  55. </style>
复制代码

哀求到的数组为空时
  1. channelList.value = []
复制代码

添加element-plus插槽:
  1.      <!-- 主体部分--表格 -->
  2.       <el-table :data="channelList" style="width: 100%" v-loading="loading">
  3.           <el-table-column   label="序号"    type="index"  width="100" ></el-table-column>
  4.           <el-table-column   label="分类名称" prop="cate_name" ></el-table-column>
  5.           <el-table-column   label="分类别名" prop="cate_alias" ></el-table-column>
  6.           <el-table-column   label="操作"      width="100">
  7.           <template #default="{row,$index}">
  8.              <el-button  @click="onEditChannel(row,$index)" plain :icon="Edit" circle type="primary" ></el-button>
  9.              <el-button  @click="onDelChannel(row,$index)" plain :icon="Delete" circle type="danger" ></el-button>
  10.           </template>
  11.           </el-table-column>
  12.          
  13.          <!-- 没有数据 -->
  14.          <template #empty>
  15.       <el-empty description="暂无数据"></el-empty>
  16.       </template>
  17.       </el-table>
复制代码

下期见!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

花瓣小跑

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表