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ù)組類型如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)物種類 * @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ù)狀表格的實(shí)現(xiàn)代碼
這篇文章主要介紹了vue中可編輯樹(shù)狀表格的實(shí)現(xiàn)代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10在vue中使用防抖和節(jié)流,防止重復(fù)點(diǎn)擊或重復(fù)上拉加載實(shí)例
今天小編就為大家分享一篇在vue中使用防抖和節(jié)流,防止重復(fù)點(diǎn)擊或重復(fù)上拉加載實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11關(guān)于vuex狀態(tài)刷新網(wǎng)頁(yè)時(shí)數(shù)據(jù)被清空問(wèn)題及解決
這篇文章主要介紹了關(guān)于vuex狀態(tài)刷新網(wǎng)頁(yè)時(shí)數(shù)據(jù)被清空問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07vue中echarts自動(dòng)輪播tooltip問(wèn)題
這篇文章主要介紹了vue中echarts自動(dòng)輪播tooltip問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10Vue.js實(shí)現(xiàn)的計(jì)算器功能完整示例
這篇文章主要介紹了Vue.js實(shí)現(xiàn)的計(jì)算器功能,結(jié)合完整實(shí)例形式分析了vue.js響應(yīng)鼠標(biāo)事件實(shí)現(xiàn)基本的數(shù)值運(yùn)算相關(guān)操作技巧,可實(shí)現(xiàn)四則運(yùn)算及乘方、開(kāi)方等功能,需要的朋友可以參考下2018-07-07