位置:- 正文

vue中的get方法\post方法如何实现传递数组参数(vue get方法)

编辑:rootadmin
vue中的get方法\post方法如何实现传递数组参数

推荐整理分享vue中的get方法\post方法如何实现传递数组参数(vue get方法),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:vue get方法,vue中get传参,vue获取get参数,vue getdata,vue getdata,vue get方法,vue getdata,vue的getters,内容如对您有帮助,希望把文章链接给更多的朋友!

1vue中get方法如何传递数组参数

直接放在对象中传递数组

export function getCrApplicationList(data) {var test = [‘111‘, ‘222‘]return request({url: ‘/applicant/CrApplication/List‘,method: ‘get‘,params: { test }})}

但是这样的话后台是取不到值的,我们需要把数组变成如下这种格式:

test:111

test:222

首先找到axios.js,加如下代码:

if (config.method === ‘get‘) {// 如果是get请求,且params是数组类型如arr=[1,2],则转换成arr=1&arr=2config.paramsSerializer = function(params) {return qs.stringify(params, { arrayFormat: ‘repeat‘ })}}vue中的get方法\post方法如何实现传递数组参数(vue get方法)

如果get请求中参数是数组格式,则数组里每一项的属性名重复使用。

同样的,post方法传get方法的传参格式时候通用该方法。

封装的接口部分:

/*** @description 以post请求方式,传递array[]数组* @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传递参数

/*** 添加动物种类* @param {*} params* @returns*/export function AddAnimalType (params) {return request({url: baseUrl + '/addAnimalType',method: 'post',data: params})}

调用代码:

下面的 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传递参数

/*** 根据Id获取详情* id id* @param {*} params* @returns*/export function selectById (params) {return request({url: baseUrl + '/selectById',method: 'get',params})}

调用接口:

//获取详情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)})},
本文链接地址:https://www.jiuchutong.com/zhishi/287997.html 转载请保留说明!
下一篇链接:https://www.jiuchutong.com/zhishi/287998.html
免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

鄂ICP备2023003026号

友情链接: 武汉网站建设 电脑维修 湖南楚通运网络