vue中g(shù)et方法\post方法如何傳遞數(shù)組參數(shù)詳解
1、vue中g(shù)et方法如何傳遞數(shù)組參數(shù)
直接放在對(duì)象中傳遞數(shù)組
export function getCrApplicationList(data) {
var test = [‘111‘, ‘222‘]
return request({
url: ‘/applicant/CrApplication/List‘,
method: ‘get‘,
params: { test }
})
}但是這樣的話后臺(tái)是取不到值的,我們需要把數(shù)組變成如下這種格式:
test:111
test:222
首先找到axios.js,加如下代碼:
if (config.method === ‘get‘) {
// 如果是get請(qǐng)求,且params是數(shù)組類(lèi)型如arr=[1,2],則轉(zhuǎn)換成arr=1&arr=2
config.paramsSerializer = function(params) {
return qs.stringify(params, { arrayFormat: ‘repeat‘ })
}
}如果get請(qǐng)求中參數(shù)是數(shù)組格式,則數(shù)組里每一項(xiàng)的屬性名重復(fù)使用。
同樣的,post方法傳get方法的傳參格式時(shí)候通用該方法。
封裝的接口部分:
/**
* @description 以post請(qǐng)求方式,傳遞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' })
}
}2、vue get與post傳參方式
vue的封裝接口中,post與get的傳參方式是不同的
2.1post:用data傳遞參數(shù)
/**
* 添加動(dòng)物種類(lèi)
* @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.2get:用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)
})
},總結(jié)
到此這篇關(guān)于vue中g(shù)et方法\post方法如何傳遞數(shù)組參數(shù)的文章就介紹到這了,更多相關(guān)vue傳遞數(shù)組參數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue實(shí)現(xiàn)商品列表的添加刪除實(shí)例講解
在本篇內(nèi)容里小編給大家分享的是關(guān)于vue實(shí)現(xiàn)商品列表的添加刪除實(shí)例講解,有興趣的朋友們可以參考下。2020-05-05
vue計(jì)算屬性和監(jiān)聽(tīng)器實(shí)例解析
本文通過(guò)基本實(shí)例給大家介紹了vue計(jì)算屬性和監(jiān)聽(tīng)器的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-05-05
vue+elementUI組件tree如何實(shí)現(xiàn)單選加條件禁用
這篇文章主要介紹了vue+elementUI組件tree如何實(shí)現(xiàn)單選加條件禁用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
vue flex 布局實(shí)現(xiàn)div均分自動(dòng)換行的示例代碼
這篇文章主要介紹了vue flex 布局實(shí)現(xiàn)div均分自動(dòng)換行,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
el-input無(wú)法輸入的問(wèn)題和表單驗(yàn)證失敗問(wèn)題解決
在做項(xiàng)目的時(shí)候發(fā)現(xiàn)一個(gè)情況,輸入框無(wú)法輸入值并且表單校驗(yàn)失靈,所以下面這篇文章主要給大家介紹了關(guān)于el-input無(wú)法輸入的問(wèn)題和表單驗(yàn)證失敗問(wèn)題解決的相關(guān)資料,需要的朋友可以參考下2023-02-02
vue中pc移動(dòng)滾動(dòng)穿透問(wèn)題及解決
這篇文章主要介紹了vue中pc移動(dòng)滾動(dòng)穿透問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
解決vue-photo-preview 異步圖片放大失效的問(wèn)題
這篇文章主要介紹了解決vue-photo-preview 異步圖片放大失效的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07
Vue實(shí)現(xiàn)可復(fù)用輪播組件的方法
這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)可復(fù)用輪播組件的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07

