亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

vue3+ts重復(fù)參數(shù)提取成方法多處調(diào)用以及字段無(wú)值時(shí)不傳字段給后端問(wèn)題

 更新時(shí)間:2024年10月16日 08:53:14   作者:性野喜悲  
在進(jìn)行API開發(fā)時(shí),優(yōu)化參數(shù)傳遞是一個(gè)重要的考量,傳統(tǒng)方法中,即使參數(shù)值為空,也會(huì)被包含在請(qǐng)求中發(fā)送給后端,這可能會(huì)導(dǎo)致不必要的數(shù)據(jù)處理,而優(yōu)化后的方法則只會(huì)傳遞那些實(shí)際有值的字段,從而提高數(shù)據(jù)傳輸?shù)挠行院秃蠖颂幚淼男?/div>

vue3+ts重復(fù)參數(shù)提取成方法多處調(diào)用以及字段無(wú)值時(shí)不傳字段給后端

參數(shù)提取前的寫法

此寫法值為空的時(shí)候也會(huì)傳空字段給后端

會(huì)把無(wú)值的空字段傳給后端

修改后的寫法

不會(huì)把沒(méi)有值的字段傳給后端

// 列表和導(dǎo)出需要傳給后端的公共參數(shù)(加 || undefined即可過(guò)濾空字段)
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
  };
};
 
// 數(shù)據(jù)獲取
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("用戶運(yùn)動(dòng)記錄", result);
  }
  state.loading = false;
};
 
// 導(dǎo)出
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("暫無(wú)數(shù)據(jù)導(dǎo)出");
  }
};

只傳有值的字段給后端

總結(jié)

具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

相關(guān)文章

最新評(píng)論