vue中的get方法\post方法如何實現(xiàn)傳遞數(shù)組參數(shù)
get方法\post方法如何傳遞數(shù)組參數(shù)
背景:項目中突然來了一個post方法傳array數(shù)組的參數(shù),且該數(shù)組參數(shù)是循環(huán)遍歷所有元素按照get方法傳參形式進行參數(shù)傳遞的,對于小白的我真的是一個打擊呀。但是為了完成任務(wù)不能放棄,so通過搜索找到了解答方式(在這里非常感謝我的小伙伴雷總的熱心幫助以及bubuko.com的解答)。
第一部分:vue中g(shù)et方法如何傳遞數(shù)組參數(shù)
直接放在對象中傳遞數(shù)組
export function getCrApplicationList(data) { var test = [‘111‘, ‘222‘] return request({ url: ‘/applicant/CrApplication/List‘, method: ‘get‘, params: { test } }) }
傳遞的參數(shù)格式如下:
但是這樣的話后臺是取不到值的,我們需要把數(shù)組變成如下這種格式:
test:111
test:222
首先找到axios.js,加如下代碼:
if (config.method === ‘get‘) { // 如果是get請求,且params是數(shù)組類型如arr=[1,2],則轉(zhuǎn)換成arr=1&arr=2 config.paramsSerializer = function(params) { return qs.stringify(params, { arrayFormat: ‘repeat‘ }) } }
如果get請求中參數(shù)是數(shù)組格式,則數(shù)組里每一項的屬性名重復(fù)使用。
效果如下:
同樣的,post方法傳get方法的傳參格式時候通用該方法。
下面列出我的接口格式及解決方法的源碼
封裝的接口部分:
/** * @description 以post請求方式,傳遞array[]數(shù)組 * @param {Array[integer]} idList * @param {integer} orderId * @return {*} */ export function doFuncTest(idListVal, orderId) { return request({ url: '/xxxx/xxx', method: 'post', baseURL: '//192.168.xxx.xxx:xxxx/xxx/xxx/xxx', params: { idList: idListVal, orderId: orderId } }) }
攔截器部分:
if (config.method === 'post') { config.paramsSerializer = function(params) { return qs.stringify(params, { arrayFormat: 'repeat' }) } }
最后的訪問地址如下:
http://192.168.xxx.xxx:xxxx/xxx/xxx/xxx/xxxx/xxx?idList=261&idList=262&orderId=59
完結(jié)!
vue get與post傳參方式
vue的封裝接口中,post與get的傳參方式是不同的
1.post:用data傳遞參數(shù)
/** ?* 添加動物種類 ?* @param {*} params? ?* @returns? ?*/ export function AddAnimalType (params) { ? return request({ ? ? url: baseUrl + '/addAnimalType', ? ? method: 'post', ? ? data: params ? }) }
調(diào)用代碼:
下面的 this.formData 是在data中定義的列表里邊包含了id等信息
? ? //新增 ? ? insertAnimalType () { ? ? ? AddAnimalType(this.formData).then(response => { ? ? ? ? if (response.status == 0) { ? ? ? ? ? successMessage(response.statusText) ? ? ? ? } ? ? ? ? else { ? ? ? ? ? errMessage(response.statusText) ? ? ? ? } ? ? ? }).catch(error => { ? ? ? ? errorCollback(error) ? ? ? }) ? ? },
2.get:用params傳遞參數(shù)
/** ?* 根據(jù)Id獲取詳情 ?* id id ?* @param {*} params? ?* @returns? ?*/ export function selectById (params) { ? return request({ ? ? url: baseUrl + '/selectById', ? ? method: 'get', ? ? params ? }) }
調(diào)用接口:
? ? //獲取詳情 ? ? getDetail () { ? ? ? selectById({ animalId: this.formData.id }).then(response => { ? ? ? ? if (response.status == 0) { ? ? ? ? ? this.formData = response.data.animalType ? ? ? ? } ? ? ? ? else { ? ? ? ? ? errMessage(response.statusText) ? ? ? ? } ? ? ? }).catch(error => { ? ? ? ? errorCollback(error) ? ? ? }) ? ? },
3.delete:params傳遞參數(shù)
export function deleteAnimalType (params) { ? return request({ ? ? url: baseUrl + '/delete', ? ? method: 'delete', ? ? params ? }) }
調(diào)用接口:
? ? todelete (id) { ? ? ? console.log('點擊操作中的刪除') ? ? ? this.$confirm('此操作將永久刪除該數(shù)據(jù),是否繼續(xù)?', '提示', { ? ? ? ? confirmButtonText: '確定', ? ? ? ? cancelButtonText: '取消', ? ? ? ? type: 'warning' ? ? ? }) ? ? ? ? .then(() => { ? ? ? ? ? deleteAnimalType({ animalIds: id }).then((response) => { ? ? ? ? ? ? if (response.status == 0) { ? ? ? ? ? ? ? successMessage(response.statusText) ? ? ? ? ? ? } else { ? ? ? ? ? ? ? errMessage(response.statusText) ? ? ? ? ? ? } ? ? ? ? ? }).catch((error) => { ? ? ? ? ? ? errorCollback(error) ? ? ? ? ? }) ? ? ? ? }) ? ? },
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue 路由meta 設(shè)置導(dǎo)航隱藏與顯示功能的示例代碼
這篇文章主要介紹了vue 路由meta 設(shè)置導(dǎo)航隱藏與顯示功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09詳解vue中v-for和v-if一起使用的替代方法template
這篇文章主要介紹了vue中v-for和v-if一起使用的替代方法template,使用的版本是vue?2.9.6和element-ui:?2.15.6,通過實例代碼給大家講解的非常詳細,需要的朋友可以參考下2022-05-05Vue3實現(xiàn)動態(tài)導(dǎo)入Excel表格數(shù)據(jù)的方法詳解
在開發(fā)工作過程中,我們會遇到各種各樣的表格數(shù)據(jù)導(dǎo)入,動態(tài)數(shù)據(jù)導(dǎo)入可以減少人為操作,減少出錯。本文為大家介紹了Vue3實現(xiàn)動態(tài)導(dǎo)入Excel表格數(shù)據(jù)的方法,需要的可以參考一下2022-11-11vue組件數(shù)據(jù)傳遞、父子組件數(shù)據(jù)獲取,slot,router路由功能示例
這篇文章主要介紹了vue組件數(shù)據(jù)傳遞、父子組件數(shù)據(jù)獲取,slot,router路由功能,結(jié)合實例形式分析了vue.js組件數(shù)據(jù)傳遞、路由相關(guān)概念、原理及相關(guān)操作技巧,需要的朋友可以參考下2019-03-03