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

只传有值的字段给后端

相关推荐
Sam90291 分钟前
【Webpack--007】处理其他资源--视频音频
前端·webpack·音视频
Code成立3 分钟前
HTML5精粹练习第1章博客
前端·html·博客·html5
架构师ZYL14 分钟前
node.js+Koa框架+MySQL实现注册登录
前端·javascript·数据库·mysql·node.js
gxhlh1 小时前
React Native防止重复点击
javascript·react native·react.js
一只小白菜~1 小时前
实现实时Web应用,使用AJAX轮询、WebSocket、还是SSE呢??
前端·javascript·websocket·sse·ajax轮询
计算机学姐1 小时前
基于python+django+vue的在线学习资源推送系统
开发语言·vue.js·python·学习·django·pip·web3.py
晓翔仔2 小时前
CORS漏洞及其防御措施:保护Web应用免受攻击
前端·网络安全·渗透测试·cors·漏洞修复·应用安全
jingling5552 小时前
后端开发刷题 | 数字字符串转化成IP地址
java·开发语言·javascript·算法
GISer_Jing3 小时前
【前后端】大文件切片上传
前端·spring boot
csdn_aspnet3 小时前
npm 安装 与 切换 淘宝镜像
前端·npm·node.js