Vue2.0 http請求以及l(fā)oading展示實(shí)例
我們需要額外兩個(gè)依賴vuex 和 axios:(還是接著上一個(gè)項(xiàng)目MyFirstProject寫)
npm i vuex axios -D
首先簡單的闡述下http請求
1、main.js 中引入axios
import axios from 'axios' Vue.prototype.$http = axios;
2、focus.vue中寫個(gè)函數(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:[] //存儲(chǔ)請求返回的數(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>
獲取成功后展示效果如圖:

我的兩個(gè)男神羨慕羨慕有沒有很帥
到此請求數(shù)據(jù)就結(jié)束了,是不是很簡單,然額接下來涉及到store就有點(diǎn)復(fù)雜了,欲知后事如何,且聽下回分解~
- Vue基于vuex、axios攔截器實(shí)現(xiàn)loading效果及axios的安裝配置
- vue2實(shí)現(xiàn)數(shù)據(jù)請求顯示loading圖
- vue實(shí)現(xiàn)圖片加載完成前的loading組件方法
- Vue 全局loading組件實(shí)例詳解
- vue+axios+element ui 實(shí)現(xiàn)全局loading加載示例
- vue-cli項(xiàng)目中使用公用的提示彈層tips或加載loading組件實(shí)例詳解
- 詳解vue使用vue-layer-mobile組件實(shí)現(xiàn)toast,loading效果
- Vue自定義全局Toast和Loading的實(shí)例詳解
- 基于Vue 實(shí)現(xiàn)一個(gè)中規(guī)中矩loading組件
- vuex+axios+element-ui實(shí)現(xiàn)頁面請求loading操作示例
相關(guān)文章
UniApp中實(shí)現(xiàn)類似錨點(diǎn)定位滾動(dòng)效果
一個(gè)uniapp小程序的項(xiàng)目,我們需要實(shí)現(xiàn)一個(gè)非常實(shí)用的功能——類似于錨點(diǎn)定位的交互效果,即在首頁中有多個(gè)tab(分類標(biāo)簽),每個(gè)tab對應(yīng)著不同的模塊,當(dāng)用戶點(diǎn)擊某個(gè)分類的tab時(shí),需要流暢地滾動(dòng)到對應(yīng)的內(nèi)容位置,提供更好的用戶體驗(yàn),2023-10-10
Vue?中?Promise?的then方法異步使用及async/await?異步使用總結(jié)
then?方法是?Promise?中?處理的是異步調(diào)用,異步調(diào)用是非阻塞式的,在調(diào)用的時(shí)候并不知道它什么時(shí)候結(jié)束,也就不會(huì)等到他返回一個(gè)有效數(shù)據(jù)之后再進(jìn)行下一步處理,這篇文章主要介紹了Vue?中?Promise?的then方法異步使用及async/await?異步使用總結(jié),需要的朋友可以參考下2023-01-01
vue3+elementui-plus實(shí)現(xiàn)一個(gè)接口上傳多個(gè)文件功能
這篇文章主要介紹了vue3+elementui-plus實(shí)現(xiàn)一個(gè)接口上傳多個(gè)文件,先使用element-plus寫好上傳組件,然后假設(shè)有個(gè)提交按鈕,點(diǎn)擊上傳文件請求接口,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-07-07

