vue axios數(shù)據(jù)請(qǐng)求get、post方法的使用
我們常用的有g(shù)et方法以及post方法,下面簡單的介紹一下這兩種請(qǐng)求方法
vue中使用axios方法我們先安裝axios這個(gè)方法
npm install --save axios
安裝之后采用按需引入的方法,哪個(gè)頁面需要請(qǐng)求數(shù)據(jù)就在哪個(gè)頁面里引入一下。
import axios from 'axios'
引入之后我們就可以進(jìn)行數(shù)據(jù)請(qǐng)求了,在methods中創(chuàng)建一個(gè)方法
1methods:{2getInfo(){3? ? ? ? let url = "url"4? ? ? ? axios.get(url).then((res)=>{5console.log(res)6})? ? ? 7? ? }?
8 }
然后我們?cè)趍ounted這個(gè)生命周期中進(jìn)行調(diào)用
1mounted(){2? ? this.getInfo()?
3 }
這樣就可以在控制臺(tái)中查看數(shù)據(jù),以上是一個(gè)簡單的get方法數(shù)據(jù)請(qǐng)求,下面繼續(xù)介紹一下post方法的使用,其實(shí)post和get的使用沒有什么區(qū)別只是再加上一個(gè)參數(shù)就可以了,看一下我們的代碼
1methods:{ 2postInfo(){ 3? ? ? ? let url = "url" 4? ? ? ? let params=new URLSearchParams();//這個(gè)方法在axios的官網(wǎng)中有介紹,除了這個(gè)方法還有qs這個(gè)方法 5? ? ? ? params.append("key",index)? 6? ? ? ? params.append("key",index) 7? ? ? ? axios.post(url,params).then((res)=>{ 8console.log(res) 9})10? ? }? ?
11 }
同樣在mounted這個(gè)生命周期中進(jìn)行調(diào)用
1mounted(){2? ? this.postInfo()
3 }