欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue中 axios delete請(qǐng)求參數(shù)操作

 更新時(shí)間:2020年08月25日 15:19:23   作者:喂,小猴子  
這篇文章主要介紹了Vue中 axios delete請(qǐng)求參數(shù)操作,具有很好的參考價(jià)值,希望對(duì)大家有所 幫助。一起跟隨小編過(guò)來(lái)看看吧

vue中axios 的delete和post,put在傳值上有點(diǎn)區(qū)別

post和put有三個(gè)參數(shù),url,data和config,所以在使用這兩個(gè)時(shí),可以寫成axios.post(api,{id:1}),axios.put(api,{id:1}),但是delete只有兩個(gè)參數(shù):url和config,data在config中,所以需要寫成 axios.delete(api,{data:{id:1}})

如果是服務(wù)端將參數(shù)當(dāng)作Java對(duì)象來(lái)封裝接收則 參數(shù)格式為:

{data: param}

var param={id:1,name:'zhangsan'}
this.$axios.delete("/ehrReferralObjPro", {data: param}).then(function(response) {
 }

如果服務(wù)端將參數(shù)當(dāng)做url 參數(shù) 接收,則格式為:{params: param},這樣發(fā)送的url將變?yōu)閔ttp:www.XXX.com?a=…&b=…

var param={id:1,name:'zhangsan'}
this.$axios.delete("/ehrReferralObjPro", {params: param}).then(function(response) {
 }

axios 數(shù)組傳值時(shí),我傳到后臺(tái)的是兩個(gè)字符串?dāng)?shù)組,但是將參數(shù)當(dāng)成url參數(shù)接收時(shí),如果是正常傳值,將數(shù)組作為一個(gè)請(qǐng)求參數(shù)傳值時(shí),后臺(tái)接口接收不到匹配的參數(shù),百度之后使用JSON.stringify(),但是使用以后,后臺(tái)多了一對(duì)雙引號(hào),最后把后臺(tái)改成對(duì)象封裝接收參數(shù),使用的第一種。

補(bǔ)充知識(shí):vue 項(xiàng)目中的this.$get,this.$post等$的用法

vue官網(wǎng)上有這么一句話

結(jié)合案例:

// 基于axios 封裝的http請(qǐng)求插件
const axios = require('axios');
 
/**
 * 以下這種方式需要調(diào)用Vue.use方法 調(diào)用的時(shí)候調(diào)用 this.$fetch, this.$post, this.$axios, this.$put, this.$del 方法
 */
function coverFormData (data) {
 return Object.keys(data).map(key => {
  let value = data[key];
  if (typeof value === 'object') {
   value = JSON.stringify(value);
  }
  return encodeURIComponent(key) + '=' + encodeURIComponent(value);
 })
}
const http = {
 install(Vue, Option) {
  axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
  if (Option) {
   // 超時(shí)設(shè)置
   axios.defaults.timeout = Option.timeout || 10000;
   // 默認(rèn)請(qǐng)求地址設(shè)置
   axios.defaults.baseURL = Option.baseURL || "";
   // 頭部設(shè)置
   if (Option.headers && typeof Option.headers === 'object') {
    for (let key in Option.headers) {
     if (!Option.headers.hasOwnProperty(key)) continue;
     axios.defaults.headers[key] = Option.headers[key];
    }
   }
   // 請(qǐng)求/響應(yīng)攔截器
   Option.inRequest && axios.interceptors.request.use(Option.inRequest, error => {
     Promise.reject(error);
   });
   Option.inResponse && axios.interceptors.response.use(Option.inResponse, error => {
     Promise.reject(error);
   });
  }
  /**
   * @param {string} url
   * @param {object} params={} 參數(shù)可以根據(jù)需要自行處理
   */
  const fetch = (url, params = {}, config = {}) => {
   const str = coverFormData(params).join('&');
   return new Promise((resolve, reject) => {
    let address = url;
    if (str) {
     address += '?' + str;
    }
    axios.get(address, config).then(res => {
     resolve(res.data);
    }).catch(error => {
     reject(error);
    });
   });
  };
 
  /**
   * @param {string} url
   * @param {object} data={} 參數(shù)可以根據(jù)需要自行處理
  
   */
  const post = (url, data = {}, config = {}) => {
   let str = coverFormData(data).join('&');
   if (config.headers && config.headers['Content-Type'] && config.headers['Content-Type'].indexOf('application/json') > -1) {
    str = JSON.parse(JSON.stringify(data));
   }
   return new Promise((resolve, reject) => {
    axios.post(url, str, config).then(res => {
     resolve(res.data);
    }).catch(error => {
     reject(error);
    });
   });
  };
 
  /**
   * @param {string} url
   * @param {object} data={} 參數(shù)可以根據(jù)需要自行處理
   */
  const put = (url, data = {}, config = {}) => {
   const str = coverFormData(data).join('&');
   return new Promise((resolve, reject) => {
    axios.put(url, str, config).then(res => {
     resolve(res.data);
    }).catch(error => {
     reject(error);
    });
   });
  };
 
  /**
   * @param {string} url
   * @param {object} params={}
   */
 
  const del = (url, config = {}) => {
   const str = coverFormData(config).join('&');
   return new Promise((resolve, reject) => {
    axios.delete(url, str).then(res => {
     resolve(res.data);
    }).catch(error => {
     reject(error);
    });
   });
  };
  const data = { axios, fetch, post, put, del };
  // 這個(gè)地方說(shuō)明了為啥使用的時(shí)候是this.$fetch, this.$post, this.$axios, this.$put, this.$del 這幾個(gè)方式
  Object.keys(data).map(item => Object.defineProperty(Vue.prototype, '$' + item, { value: data[item] }));
 }
};
 
export default http;

然后在main.js中導(dǎo)入包使用:

import http from './assets/js/http';
 
Vue.use(http, {
  timeout: 60000,
  inRequest (config) {
    config.headers['Authorization'] =
      sessionStorage.getItem('TokenType') +" "
      + sessionStorage.getItem('AccessToken');
    return config;
  }, 
  inResponse (response) {
    return response;
  }
});

之后在子組件中就可以直接使用this.$post等了

比如:

this.$post("你的url", {
    CityId: cityid,
    Type: 3
   })
    .then(res => {
     if (res.Success) {
      this.searchSecondary = res.Data;
     }
    })
    .catch(error => {
     console.log(error);
    });

以上這篇Vue中 axios delete請(qǐng)求參數(shù)操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue 實(shí)現(xiàn)tab切換保持?jǐn)?shù)據(jù)狀態(tài)

    vue 實(shí)現(xiàn)tab切換保持?jǐn)?shù)據(jù)狀態(tài)

    這篇文章主要介紹了vue 實(shí)現(xiàn)tab切換保持?jǐn)?shù)據(jù)狀態(tài),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-07-07
  • Vue3 v-bind 指令的基本用法

    Vue3 v-bind 指令的基本用法

    在 Vue 3 中,v-bind?指令用于將表達(dá)式的值綁定到 DOM 元素的屬性上,這個(gè)指令的語(yǔ)法與 Vue 2 相同,但有一些細(xì)微的變化和改進(jìn),這篇文章主要介紹了Vue3 v-bind 指令的基本用法,需要的朋友可以參考下
    2024-08-08
  • 關(guān)于vue3?解決getCurrentInstance?打包后線上環(huán)境報(bào)錯(cuò)問(wèn)題

    關(guān)于vue3?解決getCurrentInstance?打包后線上環(huán)境報(bào)錯(cuò)問(wèn)題

    這篇文章主要介紹了vue3?解決getCurrentInstance?打包后線上環(huán)境報(bào)錯(cuò)問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05
  • vue 實(shí)現(xiàn)移動(dòng)端鍵盤搜索事件監(jiān)聽(tīng)

    vue 實(shí)現(xiàn)移動(dòng)端鍵盤搜索事件監(jiān)聽(tīng)

    今天小編就為大家分享一篇vue 實(shí)現(xiàn)移動(dòng)端鍵盤搜索事件監(jiān)聽(tīng),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-11-11
  • vue element-ui實(shí)現(xiàn)動(dòng)態(tài)面包屑導(dǎo)航

    vue element-ui實(shí)現(xiàn)動(dòng)態(tài)面包屑導(dǎo)航

    這篇文章主要為大家詳細(xì)介紹了vue element-ui實(shí)現(xiàn)動(dòng)態(tài)面包屑導(dǎo)航,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • Vue3中Watch、Watcheffect、Computed的使用和區(qū)別解析

    Vue3中Watch、Watcheffect、Computed的使用和區(qū)別解析

    Watch、Watcheffect、Computed各有優(yōu)劣,選擇使用哪種方法取決于應(yīng)用場(chǎng)景和需求,watch?適合副作用操作,watchEffect適合簡(jiǎn)單的自動(dòng)副作用管理,computed?適合聲明式的派生狀態(tài)計(jì)算,本文通過(guò)場(chǎng)景分析Vue3中Watch、Watcheffect、Computed的使用和區(qū)別,感興趣的朋友一起看看吧
    2024-07-07
  • Vue3?vite配置css?的sourceMap及文件引用配置別名的過(guò)程

    Vue3?vite配置css?的sourceMap及文件引用配置別名的過(guò)程

    這篇文章主要介紹了Vue3 vite配置css的sourceMap,及文件引用配置別名的過(guò)程,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-07-07
  • vue中通過(guò)使用$attrs實(shí)現(xiàn)組件之間的數(shù)據(jù)傳遞功能

    vue中通過(guò)使用$attrs實(shí)現(xiàn)組件之間的數(shù)據(jù)傳遞功能

    組件之間傳遞數(shù)據(jù)的方式有很多種,之所以有這么多種方式,是為了滿足在不同場(chǎng)景不同條件下的使用。這篇文章主要介紹了vue中通過(guò)使用$attrs實(shí)現(xiàn)組件之間的數(shù)據(jù)傳遞,需要的朋友可以參考下
    2019-09-09
  • vue實(shí)現(xiàn)登錄驗(yàn)證碼

    vue實(shí)現(xiàn)登錄驗(yàn)證碼

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)登錄驗(yàn)證碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • element-ui如何取消el-table的hover狀態(tài)(取消高亮顯示)

    element-ui如何取消el-table的hover狀態(tài)(取消高亮顯示)

    在一個(gè)項(xiàng)目中需要對(duì)element-ui的table組件進(jìn)行一些樣式的修改,其中就包括對(duì)hover效果的處理,下面這篇文章主要給大家介紹了關(guān)于element-ui如何取消el-table的hover狀態(tài)(取消高亮顯示)的相關(guān)資料,需要的朋友可以參考下
    2022-11-11

最新評(píng)論