Vue前端后端的交互方式?axios
前言:
大家都知道,只要進行數(shù)據(jù)交互,肯定就要去請求接口,數(shù)據(jù)請求的方式有vue-resource axios fetch等方式進行數(shù)據(jù)集請求
- 1,vue-resource :官方出品,在vue2x之后已經(jīng)停止更新
- 2,axios :第三方數(shù)據(jù)請求庫
- 3, fetch:是
JavaScript
最新標準出的一個數(shù)據(jù)請求方式
今天跟大家談談我們最熟悉,也是最常用的axios
安裝:
npm install --save axios
語法
最簡單的寫法
get請求:
axios.get("請求地址?kty=val&key=val").then(()=>{ //成功的回調函數(shù) }).catch(()=>{ //失敗的回調函數(shù) })
post請求
一般寫法
axios.post("請求地址",{發(fā)送的key:發(fā)送的val,xxx:xxx}.then(()=>{ //請求成功的回調函數(shù) }).catch(()=>{ //失敗的回調函數(shù) }) )
案例:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="./node_modules/vue/dist/vue.min.js"></script> <script src="./node_modules/axios/dist/axios.min.js"></script> </head> <body> <div id="demo"> </div> </body> </html> <script> new Vue({ el:"#demo", mounted(){ axios({ url:"http://api.artgoer.cn:8084/artgoer/api/v1/user/324380/v3/topic/topicHomeByLabel?pageIndex=1&token=b544cd63-6d42-46fe-a96c-3cf96bae3113&topicId=62187", method:"GET" }).then((ok)=>{ console.log(ok); }).catch((err)=>{ console.log(err); }) } }) </script>
數(shù)據(jù)請求封裝
methods:{ axiosLink(url,method){ // 數(shù)據(jù)請求的封裝 return new Promise((resolve,reject)=>{ axios({ // es6中鍵值對一樣可以簡寫 url, method }).then((ok)=>{ // 我們需要把成功的數(shù)據(jù)交給promise resolve(ok) }).catch((err)=>{ // 我們需要把失敗的數(shù)據(jù)交給promise reject(err) }) }) }
舉例:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="./node_modules/vue/dist/vue.min.js"></script> <script src="./node_modules/axios/dist/axios.js"></script> </head> <body> <div id="demodiv"> <button @click="fun()">點我請求1</button> <button @click="funb()">點我請求2</button> </div> <script> new Vue({ el: "#demodiv", data:{ }, methods:{ axiosLink(url,method){ return new Promise((resolve,reject)=>{ axios({ url, method, }).then((ok)=>{ resolve(ok) }).catch((err)=>{ reject(err) }) }) }, fun() { this.axiosLink("http://api.artgoer.cn:8084/artgoer/api/v1/user/324380/v3/topic/topicHomeByLabel?pageIndex=1&token=b544cd63-6d42-46fe-a96c-3cf96bae3113&topicId=62187", "GET").then((ok) => { console.log(ok); }).catch((err) => { console.log(err) }) }, funb() { console.log(123); this.axiosLink("http://api.artgoer.cn:8084/artgoer/api/v1/user/324380/v3/topic/topicHomeByLabel?pageIndex=1&token=b544cd63-6d42-46fe-a96c-3cf96bae3113&topicId=62187","GET").then((ok)=>{ console.log(ok); }).catch((err)=>{ console.log(err); }) } } }) </script> </body> </html>
數(shù)據(jù)展示:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="./node_modules/vue/dist/vue.min.js"></script> <script src="./node_modules/axios/dist/axios.js"></script> </head> <body> <div id="demo"> <button @click="fun()">點擊請求數(shù)據(jù)</button> <img src="./1.gif" v-if="bool"> <ul> <li v-for="(v,i) in arr"> {{v.commentTxt}} </li> </ul> </div> </body> </html> <script> new Vue({ el:"#demo", data:{ bool:false, arr:[] }, methods: { axiosLink(url,method){ return new Promise((resolve,reject)=>{ axios({ url, method }).then((ok)=>{ resolve(ok) }).catch((err)=>{ reject(err) }) }) }, fun(){ this.bool=true this.axiosLink("http://api.artgoer.cn:8084/artgoer/api/v1/user/324380/v3/topic/topicHomeByLabel?pageIndex=1&token=b544cd63-6d42-46fe-a96c-3cf96bae3113&topicId=62187","GET").then((ok)=>{ console.log(ok.data.data.commentList); this.arr=ok.data.data.commentList this.bool=false }).catch((err)=>{ console.log(err); }) } }, }) </script>
到此這篇關于Vue前端后端的交互方式 axios的文章就介紹到這了,更多相關Vue交互方式內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
vue2.0 如何把子組件的數(shù)據(jù)傳給父組件(推薦)
這篇文章主要介紹了vue2.0 如何把子組件的數(shù)據(jù)傳給父組件,需要的朋友可以參考下2018-01-01Vue.js每天必學之計算屬性computed與$watch
Vue.js每天必學之計算屬性computed與$watch,為大家詳細講解計算屬性computed與$watch,感興趣的小伙伴們可以參考一下2016-09-09Vue通過Blob對象實現(xiàn)導出Excel功能示例代碼
這篇文章主要介紹了Vue通過Blob對象實現(xiàn)導出Excel功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07