最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue中的get方法\post方法如何實現(xiàn)傳遞數(shù)組參數(shù)

 更新時間:2022年04月24日 09:49:25   作者:怡暘  
這篇文章主要介紹了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)文章

  • vue3中proxy的基本用法說明

    vue3中proxy的基本用法說明

    這篇文章主要介紹了vue3中proxy的基本用法說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • 通過fastclick源碼分析徹底解決tap“點透”

    通過fastclick源碼分析徹底解決tap“點透”

    這篇文章主要介紹了通過fastclick源碼分析徹底解決tap“點透”問題的知識內(nèi)容,有興趣的朋友學(xué)習(xí)一下吧。
    2017-12-12
  • Vue3?在<script?setup>里設(shè)置組件name屬性的方法

    Vue3?在<script?setup>里設(shè)置組件name屬性的方法

    這篇文章主要介紹了Vue3?在<script?setup>里設(shè)置組件name屬性的方法,本文通過示例代碼給大家介紹的非常詳細,需要的朋友參考下吧
    2023-11-11
  • vue 函數(shù)調(diào)用加括號與不加括號的區(qū)別

    vue 函數(shù)調(diào)用加括號與不加括號的區(qū)別

    這篇文章主要介紹了vue 函數(shù)調(diào)用加括號與不加括號的區(qū)別,幫助大家更好的理解和使用vue,感興趣的朋友可以了解下
    2020-10-10
  • Vue使用element-ui實現(xiàn)菜單導(dǎo)航

    Vue使用element-ui實現(xiàn)菜單導(dǎo)航

    這篇文章主要為大家詳細介紹了Vue使用element-ui實現(xiàn)菜單導(dǎo)航,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • vue使用axios的小技巧分享

    vue使用axios的小技巧分享

    這篇文章主要為大家詳細介紹了一些vue使用axios的小技巧,文中的示例代碼講解詳細,對我們深入掌握vue有一定的幫助,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-10-10
  • vue2的todolist入門小項目的詳細解析

    vue2的todolist入門小項目的詳細解析

    本篇文章主要介紹了vue2的todolist入門小項目的詳細解析,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • Vue的Flux框架之Vuex狀態(tài)管理器

    Vue的Flux框架之Vuex狀態(tài)管理器

    本文內(nèi)容主要參考官方教程,為了方便理解,用更加通俗的文字講解Vuex,也原文內(nèi)容做一些重點引用。希望會對你有所幫助。
    2017-07-07
  • Vue中前端與后端如何實現(xiàn)交互

    Vue中前端與后端如何實現(xiàn)交互

    這篇文章主要介紹了Vue中前端與后端如何實現(xiàn)交互,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • Vue自定義指令封裝節(jié)流函數(shù)的方法示例

    Vue自定義指令封裝節(jié)流函數(shù)的方法示例

    本篇文章主要介紹了Vue自定義指令封裝節(jié)流函數(shù)的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-07-07

最新評論

潞城市| 亚东县| 应城市| 遂川县| 禄丰县| 农安县| 南溪县| 泽库县| 惠安县| 尚义县| 太康县| 鄂尔多斯市| 鄢陵县| 雷波县| 依安县| 重庆市| 家居| 广宁县| 岳西县| 资兴市| 大理市| 新绛县| 姜堰市| 巩义市| 垫江县| 南岸区| 焦作市| 岫岩| 保定市| 楚雄市| 平南县| 肇东市| 平阴县| 湟中县| 邓州市| 浙江省| 吐鲁番市| 登封市| 衡阳市| 炉霍县| 大姚县|