欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

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

此寫法值為空的時候也會傳空字段給后端

會把無值的空字段傳給后端

修改后的寫法

不會把沒有值的字段傳給后端

// 列表和導(dǎo)出需要傳給后端的公共參數(shù)(加 || 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
  };
};
 
// 數(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("用戶運動記錄", 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("暫無數(shù)據(jù)導(dǎo)出");
  }
};

只傳有值的字段給后端

總結(jié)

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

相關(guān)文章

最新評論