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)文章
Vue中實現(xiàn)3D標(biāo)簽云的詳細(xì)代碼
本文通過實例代碼給大家介紹vue實現(xiàn)3D標(biāo)簽云的方法,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2021-07-07uniapp組件uni-file-picker中設(shè)置使用照相機和相冊權(quán)限的操作方法
這篇文章主要介紹了uniapp組件uni-file-picker中設(shè)置使用照相機和相冊的權(quán)限,在uniapp中,我們通常會使用uni-file-picker這個組件,但是這個組件中,有點缺陷,就是沒有對這個功能的傳值設(shè)置,這里就要給組件進(jìn)行修改了,需要的朋友可以參考下2022-11-11vue mint-ui 實現(xiàn)省市區(qū)街道4級聯(lián)動示例(仿淘寶京東收貨地址4級聯(lián)動)
本篇文章主要介紹了vue mint-ui 實現(xiàn)省市區(qū)街道4級聯(lián)動(仿淘寶京東收貨地址4級聯(lián)動),非常具有實用價值,需要的朋友可以參考下2017-10-10詳解vue 配合vue-resource調(diào)用接口獲取數(shù)據(jù)
本篇文章主要介紹了vue 配合vue-resource調(diào)用接口獲取數(shù)據(jù),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06