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(()=>{
//成功的回調(diào)函數(shù)
}).catch(()=>{
//失敗的回調(diào)函數(shù)
})post請求
一般寫法
axios.post("請求地址",{發(fā)送的key:發(fā)送的val,xxx:xxx}.then(()=>{
//請求成功的回調(diào)函數(shù)
}).catch(()=>{
//失敗的回調(diào)函數(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交互方式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
vue 使用 echarts 繪制中國地圖的實現(xiàn)代碼
這篇文章主要介紹了vue 使用 echarts 繪制中國地圖,內(nèi)容包括插入echarts所需模塊及完整的代碼,代碼簡單易懂,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-01-01
elementUI?checkBox報錯Cannot read property &ap
這篇文章主要為大家介紹了elementUI?checkBox報錯Cannot read property 'length' of undefined的解決分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06
vue-router二級導航切換路由及高亮顯示的實現(xiàn)方法
這篇文章主要給大家介紹了關于vue-router二級導航切換路由及高亮顯示的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Vue具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-07-07
vue使用百度地圖報錯BMap?is?not?defined問題及解決
這篇文章主要介紹了vue使用百度地圖報錯BMap?is?not?defined問題及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10

