小編給大家分享一下vue中如何使用axios數(shù)據(jù)請求get、post方法,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
創(chuàng)新互聯(lián)是一家集網(wǎng)站建設,撫寧企業(yè)網(wǎng)站建設,撫寧品牌網(wǎng)站建設,網(wǎng)站定制,撫寧網(wǎng)站建設報價,網(wǎng)絡營銷,網(wǎng)絡優(yōu)化,撫寧網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學習、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。
vue中使用axios方法我們先安裝axios這個方法
npm install --save axios
安裝之后采用按需引入的方法,哪個頁面需要請求數(shù)據(jù)就在哪個頁面里引入一下。
import axios from 'axios'
引入之后我們就可以進行數(shù)據(jù)請求了,在methods中創(chuàng)建一個方法
methods:{ getInfo(){ let url = "url" axios.get(url).then((res)=>{ console.log(res) }) } }
然后我們在mounted這個生命周期中進行調(diào)用
mounted(){ this.getInfo() }
這樣就可以在控制臺中查看數(shù)據(jù),以上是一個簡單的get方法數(shù)據(jù)請求,下面繼續(xù)介紹一下post方法的使用,其實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這個生命周期中進行調(diào)用
mounted(){ this.postInfo() }
補充:下面看下axios 數(shù)據(jù)請求
項目地址: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è)務中經(jīng)常將其封裝為實例形式調(diào)用,便于通用配置:
// util.js const 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 })
強求攔截可用于loading..
axios.interceptors.request.use(config=>{ console.log("require init"); return config }) axios.interceptors.response.use(response=>{ console.log("response init"); return response })
看完了這篇文章,相信你對“vue中如何使用axios數(shù)據(jù)請求get、post方法”有了一定的了解,如果想了解更多相關知識,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
文章題目:vue中如何使用axios數(shù)據(jù)請求get、post方法
當前URL:http://chinadenli.net/article16/gohegg.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷、網(wǎng)站改版、網(wǎng)站設計、網(wǎng)站收錄、定制網(wǎng)站、網(wǎng)站維護
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)