当前位置: 首页 > news >正文

Vue3 + Element-Plus 使用 Table 插槽时数据未及时更新

Vue3 + Element-Plus 使用 Table 插槽时数据未及时更新

  • 问题重现
  • 解决方法
  • 最终效果

问题重现


这里我已经通过二级分类 id 查询到一级分类和二级分类,但是使用插槽和 v-for 渲染出来还是之前的分类 id,但是一点击表格或者保存代码他又能正常刷新出来。

在这里插入图片描述

<template><el-table :height="tableHeight" :data="tableList" border stripe><el-table-column prop="goodsDesc" label="商品简介" align="center"></el-table-column><el-table-column prop="categoryId" label="分类" align="center"><template #default="scope" style="display: flex;"><el-tag style="margin-left: 10px;" type="success" effect="dark" v-for="(item, index) in scope.row.categoryId" :key="index">{{ item }}</el-tag></template></el-table-column></el-table>
</template><script setup lang="ts">
import { onMounted, ref } from 'vue';
// 表格数据
const tableList = ref([]);
// 列表查询
const getList = async () => {let res = await getGoodsListApi(searchParm, "0");if (res && res.code == 200) {// 获取出图片和视频列表res.data.records.forEach((goods: any) => {// 分割 image 属性,得到图片和视频链接数组// 通过二级分类标签获取整个分类类别let res = getGoodsTypeBySonIdApi(goods.categoryId);res.then((value) => {goods.categoryId = value.data;});const imageArray = goods.image.split(',');// 判断每个链接的类型并添加对应的属性imageArray.forEach((url: String) => {if (url.endsWith('.png') || url.endsWith('.jpg') || url.endsWith('.jpeg')) {// 图片链接if (!goods.images) {goods.images = [];}goods.images.push(url);} else if (url.endsWith('.mp4') || url.endsWith('.avi') || url.endsWith('.mov')) {// 视频链接if (!goods.videos) {goods.videos = [];}goods.videos.push(url);}});// 删除原始的 image 属性delete goods.image;});tableList.value = res.data.records;searchParm.total = res.data.total;}
}
onMounted(() => {tableHeight.value = window.innerHeight - 200;getList();
})

处理后的数据示例

[{"goodsId": 3,"userId": 7,"orderId": null,"categoryId": ["数码","电脑"],"type": "0","goodsName": null,"goodsDesc": "这是二条测试数据","goodsOldPrice": 200,"goodsNewPrice": 100,"isNew": null,"tradingType": "邮寄","findType": "QQ","findValue": "13036497562","wxNum": 0,"status": "1","sellStatus": null,"createTime": "2024-04-14","sellTime": null,"address": "北京市东城区东华门街道锡拉胡同北京利生体育商厦, 39.916405, 116.410243","deleteStatus": "0","userById": 0,"images": [],"videos": []},{"goodsId": 1,"userId": 7,"orderId": null,"categoryId": ["数码","电脑"],"type": "0","goodsName": null,"goodsDesc": "这是一条测试数据","goodsOldPrice": 200,"goodsNewPrice": 100,"isNew": null,"tradingType": "面交","findType": "手机号","findValue": "13036497562","wxNum": 0,"status": "1","sellStatus": null,"createTime": "2024-04-14","sellTime": null,"address": "北京市东城区东华门街道锡拉胡同北京利生体育商厦, 39.916405, 116.410243","deleteStatus": null,"userById": 0,"images": [],"videos": []}
]

解决方法


通过二级分类标签获取整个分类类别
不与
获取出图片和视频列表操作
同时进行

// 列表查询
const getList = async () => {let res = await getGoodsListApi(searchParm, "0");if (res && res.code == 200) {// 获取出图片和视频列表res.data.records.forEach((goods: any) => {// 分割 image 属性,得到图片和视频链接数组const imageArray = goods.image.split(',');// 判断每个链接的类型并添加对应的属性imageArray.forEach((url: String) => {if (url.endsWith('.png') || url.endsWith('.jpg') || url.endsWith('.jpeg')) {// 图片链接if (!goods.images) {goods.images = [];}goods.images.push(url);} else if (url.endsWith('.mp4') || url.endsWith('.avi') || url.endsWith('.mov')) {// 视频链接if (!goods.videos) {goods.videos = [];}goods.videos.push(url);}});// 删除原始的 image 属性delete goods.image;});tableList.value = res.data.records;searchParm.total = res.data.total;getAllType();}
}// 通过二级分类标签获取整个分类类别
const getAllType = () => {tableList.value.forEach((goods: any) => {// 通过二级分类标签获取整个分类类别let res = getGoodsTypeBySonIdApi(goods.categoryId);res.then((value) => {goods.categoryId = value.data;  });})
}

最终效果

在这里插入图片描述

http://www.lryc.cn/news/339910.html

相关文章:

  • vue 2 怎么把2024-04-13T17:42:19转换成短日期格式
  • 网络IO模型以及实际应用
  • 一文详解MES、ERP、SCM、WMS、APS、SCADA、PLM、QMS、CRM、EAM及其关系
  • 《Kubernetes部署篇:基于Kylin V10+ARM架构CPU使用containerd部署K8S 1.26.15集群(一主多从)》
  • maven命令
  • jetson系列开发板使用虚拟机烧录系统时,遇见无法识别开发板的情况
  • 【数据结构】树与二叉树、树与森林部分习题以及算法设计例题 2
  • Cesium之home键开关及相机位置设置
  • FreeRTOS_day1
  • Nginx日志格式化和追踪
  • 华为交换机配置telnet SSH登录步骤
  • 市面上加密混淆软件的比较和推荐
  • 最新AI创作系统ChatGPT网站源码AI绘画,GPTs,AI换脸支持,GPT联网提问、DALL-E3文生图
  • 电视盒子哪个好?2024口碑网络电视盒子排行榜
  • CookieSession
  • Nginx服务 重写功能与反向代理
  • Midjourney教程(完整版)-看这篇就够了
  • 服务器上部署GPU版的milvus向量数据库
  • 【配置】Docker安装可道云网盘
  • 复盘中得道,技术人的自由之路
  • Nginx配置大全【六大使用场景、七大负载均衡策略、四大负载健康检查】
  • GDPU Java 天码行空8
  • 《前端面试题》- JS基础 - 伪数组
  • TypeScript 基础语法
  • 服务器数据恢复—V7000存储raid5数据恢复案例
  • 扫雷 【搜索,哈希】
  • 如何在CentOS安装Firefox并结合内网穿透工具实现公网访问本地火狐浏览器
  • LlamaIndex 组件 - Loading
  • 再见了 wordpress !又一款简洁实用的个人博客,简单好使【文末领福利】
  • 【经典算法】LeetCode 136:只出现一次的数字(Java/C/Python3实现含注释说明,Easy)