vue中get方法和post方法怎么传递数组参数

其他教程   发布日期:2023年10月16日   浏览次数:612

这篇文章主要介绍了vue中get方法和post方法怎么传递数组参数的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇vue中get方法和post方法怎么传递数组参数文章都会有所收获,下面我们一起来看看吧。

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

直接放在对象中传递数组

  1. export function getCrApplicationList(data) {
  2. var test = [‘111‘, 222‘]
  3. return request({
  4. url: ‘/applicant/CrApplication/List‘,
  5. method: get‘,
  6. params: { test }
  7. })
  8. }

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

test:111

test:222

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

  1. if (config.method === get‘) {
  2. // 如果是get请求,且params是数组类型如arr=[1,2],则转换成arr=1&arr=2
  3. config.paramsSerializer = function(params) {
  4. return qs.stringify(params, { arrayFormat: repeat })
  5. }
  6. }

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

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

封装的接口部分:

  1. /**
  2. * @description 以post请求方式,传递array[]数组
  3. * @param {Array[integer]} idList
  4. * @param {integer} orderId
  5. * @return {*}
  6. */
  7. export function doFuncTest(idListVal, orderId) {
  8. return request({
  9. url: '/xxxx/xxx',
  10. method: 'post',
  11. baseURL: '//192.168.xxx.xxx:xxxx/xxx/xxx/xxx',
  12. params: {
  13. idList: idListVal,
  14. orderId: orderId
  15. }
  16. })
  17. }

拦截器部分:

  1. if (config.method === 'post') {
  2. config.paramsSerializer = function(params) {
  3. return qs.stringify(params, { arrayFormat: 'repeat' })
  4. }
  5. }

2、vue get与post传参方式

vue的封装接口中,post与get的传参方式是不同的

2.1post:用data传递参数

  1. /**
  2. * 添加动物种类
  3. * @param {*} params
  4. * @returns
  5. */
  6. export function AddAnimalType (params) {
  7. return request({
  8. url: baseUrl + '/addAnimalType',
  9. method: 'post',
  10. data: params
  11. })
  12. }

调用代码:

下面的 this.formData 是在data中定义的列表里边包含了id等信息

  1. //新增
  2. insertAnimalType () {
  3. AddAnimalType(this.formData).then(response => {
  4. if (response.status == 0) {
  5. successMessage(response.statusText)
  6. }
  7. else {
  8. errMessage(response.statusText)
  9. }
  10. }).catch(error => {
  11. errorCollback(error)
  12. })
  13. },

2.2get:用params传递参数

  1. /**
  2. * 根据Id获取详情
  3. * id id
  4. * @param {*} params
  5. * @returns
  6. */
  7. export function selectById (params) {
  8. return request({
  9. url: baseUrl + '/selectById',
  10. method: 'get',
  11. params
  12. })
  13. }

调用接口:

  1. //获取详情
  2. getDetail () {
  3. selectById({ animalId: this.formData.id }).then(response => {
  4. if (response.status == 0) {
  5. this.formData = response.data.animalType
  6. }
  7. else {
  8. errMessage(response.statusText)
  9. }
  10. }).catch(error => {
  11. errorCollback(error)
  12. })
  13. },

附:uniapp使用uview报错没找到该组件或者要求你检查easycom规范

这都是pagej.soneasycom的错误:

使用Uview-ui组件时,如果是npm安装 需要在page.json中修改easycom配置,

如果是下载安装,则需要有@/ 如果是npm安装 则去掉@/,使用cnpm则无法使用 重新使用npm或者 下载安装

注意:改正后一定重启HBx!!!

以上就是vue中get方法和post方法怎么传递数组参数的详细内容,更多关于vue中get方法和post方法怎么传递数组参数的资料请关注九品源码其它相关文章!