1 vue中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=2
    config.paramsSerializer = function(params) {
      return qs.stringify(params, { arrayFormat: ‘repeat‘ })
 

如果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)
Logo

基于 Vue 的企业级 UI 组件库和中后台系统解决方案,为数万开发者服务。

更多推荐

  • 浏览量 2466
  • 收藏 0
  • 0

所有评论(0)