溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

vue中g(shù)et方法和post方法怎么傳遞數(shù)組參數(shù)

發(fā)布時(shí)間:2023-03-23 13:51:13 來源:億速云 閱讀:177 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹了vue中g(shù)et方法和post方法怎么傳遞數(shù)組參數(shù)的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇vue中g(shù)et方法和post方法怎么傳遞數(shù)組參數(shù)文章都會(huì)有所收獲,下面我們一起來看看吧。

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)
      })
    },

附:uniapp使用uview報(bào)錯(cuò)沒找到該組件或者要求你檢查easycom規(guī)范

這都是pagej.soneasycom的錯(cuò)誤:

使用Uview-ui組件時(shí),如果是npm安裝 需要在page.json中修改easycom配置,

如果是下載安裝,則需要有@/  如果是npm安裝 則去掉@/,使用cnpm則無法使用 重新使用npm或者 下載安裝

注意:改正后一定重啟HBx!??!

關(guān)于“vue中g(shù)et方法和post方法怎么傳遞數(shù)組參數(shù)”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“vue中g(shù)et方法和post方法怎么傳遞數(shù)組參數(shù)”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI