vue3+ts 重复参数提取成方法多处调用以及字段无值时不传字段给后端

参数提取前的写法,此写法值为空的时候也会传空字段给后端

会把无值的空字段传给后端

修改后的写法,不会把没有值的字段传给后端

复制代码
// 列表和导出需要传给后端的公共参数(加 || undefined即可过滤空字段)
const getCurentParam = () => {
  return {
    user_id: info.id || undefined,
    shop_id: state.shop_id || undefined,
    equipment_type: state.equipment_type || undefined,
    relate_type: state.relate_type || undefined,
    sdate: state.dateTime ? state.dateTime[0] : undefined,
    edate: state.dateTime ? state.dateTime[1] : undefined
  };
};

// 数据获取
const getTableData = async () => {
  state.loading = true;
  const { code, data, total } = (await xcx_user_sportlog({
    ...getCurentParam(),
    page: state.pageIndex,
    size: state.pageSize
  })) as HttpResponse;
  if (code == 200 && data) {
    let result = data || [];
    state.tableData = result;
    state.total = total;
    console.log("用户运动记录", result);
  }
  state.loading = false;
};

// 导出
const onExport = async () => {
  const res = (await export_sportlog(getCurentParam())) as HttpResponse;
  if (res.code == 200 && res?.url) {
    const link = document.createElement("a");
    link.href = res?.url;
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
  } else {
    ElMessage.warning("暂无数据导出");
  }
};

只传有值的字段给后端

相关推荐
用户44455436542623 分钟前
Android依赖的统一管理
前端
国家二级编程爱好者23 分钟前
Android Lottie使用,如何自定义LottieView?
android·前端
南囝coding24 分钟前
《独立开发者精选工具》第 025 期
前端·后端
@淡 定25 分钟前
Dubbo + Nacos 完整示例项目
前端·chrome·dubbo
毕设源码-邱学长28 分钟前
【开题答辩全过程】以 基于web的博客论坛系统的设计与实现为例,包含答辩的问题和答案
前端
就叫曲奇饼干吧29 分钟前
前端面试题整理(方便自己看的)
前端·面试
拖拉斯旋风1 小时前
防抖(Debounce)实战解析:如何用闭包优化频繁 AJAX 请求,提升用户体验
前端
老前端的功夫1 小时前
TypeScript 全局类型声明:declare关键字的深度解析与实战
linux·前端·javascript·ubuntu·typescript·前端框架
golang学习记1 小时前
VS Code 1.107 发布:AI 不再是插件,而是编辑器的「第一大脑」
前端
EndingCoder1 小时前
TypeScript 入门:理解其本质与价值
前端·javascript·ubuntu·typescript·node.js