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("暂无数据导出");
  }
};

只传有值的字段给后端

相关推荐
景彡先生23 分钟前
Python Selenium详解:从入门到实战,Web自动化的“瑞士军刀”
前端·python·selenium
Liudef062 小时前
DeepseekV3.2 实现构建简易版Wiki系统:从零开始的HTML实现
前端·javascript·人工智能·html
景早3 小时前
vue 记事本案例详解
前端·javascript·vue.js
wangjialelele5 小时前
Qt中的常用组件:QWidget篇
开发语言·前端·c++·qt
乔冠宇5 小时前
vue需要学习的点
前端·vue.js·学习
用户47949283569155 小时前
同样是 #,锚点和路由有什么区别
前端·javascript
Hero_11275 小时前
在pycharm中install不上需要的包
服务器·前端·pycharm
爱上妖精的尾巴5 小时前
5-26 WPS JS宏数组元素添加删除应用
开发语言·前端·javascript·wps·js宏
是谁眉眼5 小时前
wpsapi
前端·javascript·html
谅望者5 小时前
Flexbox vs Grid:先学哪一个?CSS 布局完全指南(附可视化示例)
前端·css·html·css3·css布局·css flexbox·css grid