最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue前端后端的交互方式?axios

 更新時間:2022年04月25日 08:39:53   作者:清城幻影  
這篇文章主要介紹了Vue前端后端的交互方式?axios,axios?為第三方數(shù)據(jù)請求庫,下文具體的內(nèi)容介紹需要的小伙伴可以參考一下,希望對你的學習有所幫助

前言:

大家都知道,只要進行數(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列表渲染的示例代碼

    Vue列表渲染的示例代碼

    這篇文章主要介紹了Vue列表渲染的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-11-11
  • vue 使用 echarts 繪制中國地圖的實現(xiàn)代碼

    vue 使用 echarts 繪制中國地圖的實現(xiàn)代碼

    這篇文章主要介紹了vue 使用 echarts 繪制中國地圖,內(nèi)容包括插入echarts所需模塊及完整的代碼,代碼簡單易懂,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-01-01
  • vue全局過濾器概念及注意事項和基本使用方法

    vue全局過濾器概念及注意事項和基本使用方法

    這篇文章主要給大家分享了vue全局過濾器概念及注意事項和基本使用方法,下面文字圍繞vue全局過濾器的相關資料展開具體的詳細內(nèi)容,需要的朋友可以參考一下,希望對你有所幫助
    2021-11-11
  • elementUI?checkBox報錯Cannot read property 'length' of undefined解決

    elementUI?checkBox報錯Cannot read property &ap

    這篇文章主要為大家介紹了elementUI?checkBox報錯Cannot read property 'length' of undefined的解決分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-06-06
  • vue3實現(xiàn)淘寶放大鏡效果的示例代碼

    vue3實現(xiàn)淘寶放大鏡效果的示例代碼

    放大鏡效果在很多購物網(wǎng)站都可以看到,本文主要介紹了vue3實現(xiàn)淘寶放大鏡效果的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-06-06
  • vue?filter的幾種用法示例小結(jié)

    vue?filter的幾種用法示例小結(jié)

    Vue.filter函數(shù)用于注冊一個全局的過濾器,該過濾器可以在模板和組件中使用,這篇文章主要介紹了vue?filter的幾種用法示例小結(jié),需要的朋友可以參考下
    2024-08-08
  • vue-router二級導航切換路由及高亮顯示的實現(xiàn)方法

    vue-router二級導航切換路由及高亮顯示的實現(xiàn)方法

    這篇文章主要給大家介紹了關于vue-router二級導航切換路由及高亮顯示的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Vue具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-07-07
  • vue使用百度地圖報錯BMap?is?not?defined問題及解決

    vue使用百度地圖報錯BMap?is?not?defined問題及解決

    這篇文章主要介紹了vue使用百度地圖報錯BMap?is?not?defined問題及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • Vue computed計算屬性詳細講解

    Vue computed計算屬性詳細講解

    computed是vue的配置選項,它的值是一個對象,其中可定義多個計算屬性,每個計算屬性就是一個函數(shù),下面這篇文章主要給大家介紹了關于vue中計算屬性computed的詳細講解,需要的朋友可以參考下
    2022-10-10
  • vue--點擊當前增加class,其他刪除class的方法

    vue--點擊當前增加class,其他刪除class的方法

    今天小編就為大家分享一篇vue--點擊當前增加class,其他刪除class的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09

最新評論

平湖市| 盖州市| 南岸区| 南和县| 祥云县| 隆回县| 民和| 洪湖市| 牡丹江市| 慈利县| 凤冈县| 娱乐| 金山区| 新邵县| 邳州市| 通榆县| 永康市| 内丘县| 类乌齐县| 彰化市| 丰城市| 舒兰市| 塔城市| 南郑县| 茌平县| 黔江区| 克什克腾旗| 广西| 天气| 太康县| 兰坪| 镇原县| 南充市| 龙口市| 壶关县| 观塘区| 南华县| 镇赉县| 昌都县| 南阳市| 永济市|