Vue2.0 http請求以及l(fā)oading展示實例
更新時間:2018年03月06日 08:38:41 作者:SherrySherryBell
下面小編就為大家分享一篇Vue2.0 http請求以及l(fā)oading展示實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
我們需要額外兩個依賴vuex 和 axios:(還是接著上一個項目MyFirstProject寫)
npm i vuex axios -D
首先簡單的闡述下http請求
1、main.js 中引入axios
import axios from 'axios' Vue.prototype.$http = axios;
2、focus.vue中寫個函數(shù)獲取數(shù)據(jù)
<template>
<div id="focus">
<ul >
<li v-for="(item,index) in focusList">
<div class="fportraits">
<img :src="'./src/'+item.portrait" :alt="item.name">
</div>
<div class="details">
<div class="ftitle"><strong> {{ item.name }} </strong></div>
<p> {{ item.production }} </p>
</div>
<div class="isfocused">
<p>取消關(guān)注</p>
</div>
<div class="clearfix"></div>
</li>
</ul>
</div>
</template>
<script>
export default{
data(){
return {
focusList:[] //存儲請求返回的數(shù)據(jù)
}
},
mounted(){
this.getFocusList()
},
methods:{
getFocusList(){ //http get請求data.json 的數(shù)據(jù)
var vm = this;
this.$http.get('src/assets/data/data.json')
.then(function(res){
vm.focusList = res.data;
})
.catch(function(err){
console.log(err)
})
}
}
}
</script>
<style scoped>
#focus{text-align:left;}
#focus ul{margin:0 auto;width:50rem;border-bottom:none;}
#focus p{margin:0;}
#focus li{width:46rem;display:block;border-bottom:1px solid #ddd;padding:0.5rem 2rem;cursor:default;}
#focus img{height:4rem;margin-left:-1rem;}
.fportraits{float:left;width:4rem;height:4rem;border-radius:50%;overflow:hidden;}
.details{float:left;margin-left:1rem;}
.isfocused{float:right;font-size:0.8rem;height:0.8rem;line-height:0.8rem;margin:0;}
.clearfix{clear:both;}
</style>
獲取成功后展示效果如圖:

我的兩個男神羨慕羨慕有沒有很帥
到此請求數(shù)據(jù)就結(jié)束了,是不是很簡單,然額接下來涉及到store就有點復雜了,欲知后事如何,且聽下回分解~
您可能感興趣的文章:
- Vue基于vuex、axios攔截器實現(xiàn)loading效果及axios的安裝配置
- vue2實現(xiàn)數(shù)據(jù)請求顯示loading圖
- vue實現(xiàn)圖片加載完成前的loading組件方法
- Vue 全局loading組件實例詳解
- vue+axios+element ui 實現(xiàn)全局loading加載示例
- vue-cli項目中使用公用的提示彈層tips或加載loading組件實例詳解
- 詳解vue使用vue-layer-mobile組件實現(xiàn)toast,loading效果
- Vue自定義全局Toast和Loading的實例詳解
- 基于Vue 實現(xiàn)一個中規(guī)中矩loading組件
- vuex+axios+element-ui實現(xiàn)頁面請求loading操作示例
相關(guān)文章
Vue?中?Promise?的then方法異步使用及async/await?異步使用總結(jié)
then?方法是?Promise?中?處理的是異步調(diào)用,異步調(diào)用是非阻塞式的,在調(diào)用的時候并不知道它什么時候結(jié)束,也就不會等到他返回一個有效數(shù)據(jù)之后再進行下一步處理,這篇文章主要介紹了Vue?中?Promise?的then方法異步使用及async/await?異步使用總結(jié),需要的朋友可以參考下2023-01-01
vue3+elementui-plus實現(xiàn)一個接口上傳多個文件功能
這篇文章主要介紹了vue3+elementui-plus實現(xiàn)一個接口上傳多個文件,先使用element-plus寫好上傳組件,然后假設有個提交按鈕,點擊上傳文件請求接口,本文結(jié)合實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2023-07-07

