国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

vue axios數(shù)據(jù)請求get、post方法及實(shí)例詳解

2019-11-19 12:58:20
字體:
供稿:網(wǎng)友

我們常用的有g(shù)et方法以及post方法,下面簡單的介紹一下這兩種請求方法

vue中使用axios方法我們先安裝axios這個方法

npm install --save axios

安裝之后采用按需引入的方法,哪個頁面需要請求數(shù)據(jù)就在哪個頁面里引入一下。

import axios from 'axios'

引入之后我們就可以進(jìn)行數(shù)據(jù)請求了,在methods中創(chuàng)建一個方法

 methods:{   getInfo(){     let url = "url"     axios.get(url).then((res)=>{       console.log(res)     })      }  }

然后我們在mounted這個生命周期中進(jìn)行調(diào)用

 mounted(){   this.getInfo()  }

這樣就可以在控制臺中查看數(shù)據(jù),以上是一個簡單的get方法數(shù)據(jù)請求,下面繼續(xù)介紹一下post方法的使用,其實(shí)post和get的使用沒有什么區(qū)別只是再加上一個參數(shù)就可以了,看一下我們的代碼

 methods:{   postInfo(){     let url = "url"     let params=new URLSearchParams();//這個方法在axios的官網(wǎng)中有介紹,除了這個方法還有qs這個方法     params.append("key",index)      params.append("key",index)     axios.post(url,params).then((res)=>{       console.log(res)     })   }   }

同樣在mounted這個生命周期中進(jìn)行調(diào)用

 mounted(){   this.postInfo() }

補(bǔ)充:下面看下axios 數(shù)據(jù)請求

項(xiàng)目地址:https://github.com/axios/axios

axios是一個基于Promise,同時支持瀏覽器端和Node.js的HTTP庫,常用于Ajax請求。

Vue.js 不像jQuery 或 AngularJS,本身并沒有帶Ajax方法,因此需要借助插件或第三方HTTP庫。

GET和POST請求

 axios.get("./package.json",{     params:{      userId:"999"     },     headers:{      token:"jack"     }    }).then(res=>{     this.msg = res.data;    }).catch(function (error) {     console.log("error init."+error)    });

POST:

<code class="language-javascript"> axios.post("./package.json",{      userId:"888"     },{      headers:{       token:"tom"      }     }).then(res=>{      this.msg =res.data     }).catch(err=>{       console.log(err)     })</code> 

基于Promise 可以執(zhí)行多個并發(fā)請求:1

 function getUserAccount(){    return axios.get('/user/123')   }   function getUserPermissions(){    return axios.get('/user/12345/premissions')   }   axios.all([getUserAccount(),getUserPermissions()])   .then(axios.spread(function(acct,perms){     //請求都完時   }))

也可通過寫入配置的形式發(fā)起請求:

  axios({  method:'post',  url:'/user/123',  data:{   firstName:'Fred',   lastName:'Flintstone'  }  }).then(function(res){  console.log(res)  })

在業(yè)務(wù)中經(jīng)常將其封裝為實(shí)例形式調(diào)用,便于通用配置:

// util.jsconst instance = axios.create({ baseURL:"http://jsonplaceholder.typicode.com/", timeout:1000, headers:{"Content-Type":"application/x-www-form-urlencoded"}})export default instance;

在mounted中調(diào)用:

Ajax({    method:'post',    url:'/package.json',    data:{    firstName:'Fred',    lastName:'flintone'    }   }).then(res=>{     console.log(res.data)     this.msg = res.data   })

強(qiáng)求攔截可用于loading..

axios.interceptors.request.use(config=>{    console.log("require init");    return config   })    axios.interceptors.response.use(response=>{    console.log("response init");    return response   })

總結(jié)

以上所述是小編給大家介紹的vue axios數(shù)據(jù)請求get、post方法及實(shí)例詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對武林網(wǎng)網(wǎng)站的支持!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 太康县| 柏乡县| 湟中县| 封开县| 邛崃市| 昌宁县| 简阳市| 河曲县| 灵川县| 青海省| 内丘县| 深泽县| 剑川县| 邯郸县| 遂宁市| 噶尔县| 宜良县| 安陆市| 海城市| 屏山县| 建湖县| 河北省| 汉川市| 保山市| 麻江县| 阳原县| 延庆县| 青州市| 寿宁县| 漳州市| 荆门市| 天祝| 铜山县| 含山县| 罗源县| 宜昌市| 隆子县| 东乌珠穆沁旗| 新余市| 龙岩市| 孙吴县|