Vue2.0 http請(qǐng)求以及l(fā)oading展示實(shí)例
我們需要額外兩個(gè)依賴vuex 和 axios:(還是接著上一個(gè)項(xiàng)目MyFirstProject寫(xiě))
npm i vuex axios -D
首先簡(jiǎn)單的闡述下http請(qǐng)求
1、main.js 中引入axios
import axios from 'axios' Vue.prototype.$http = axios;
2、focus.vue中寫(xiě)個(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ǔ)請(qǐng)求返回的數(shù)據(jù) } }, mounted(){ this.getFocusList() }, methods:{ getFocusList(){ //http get請(qǐng)求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è)男神羨慕羨慕有沒(méi)有很帥
到此請(qǐng)求數(shù)據(jù)就結(jié)束了,是不是很簡(jiǎn)單,然額接下來(lái)涉及到store就有點(diǎn)復(fù)雜了,欲知后事如何,且聽(tīng)下回分解~
- Vue基于vuex、axios攔截器實(shí)現(xiàn)loading效果及axios的安裝配置
- vue2實(shí)現(xiàn)數(shù)據(jù)請(qǐng)求顯示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)頁(yè)面請(qǐng)求loading操作示例
相關(guān)文章
vue如何解決數(shù)據(jù)加載時(shí),插值表達(dá)式閃爍問(wèn)題
這篇文章主要介紹了vue如何解決數(shù)據(jù)加載時(shí),插值表達(dá)式閃爍問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01vue+element UI中如何給指定日期添加標(biāo)記
這篇文章主要介紹了vue+element UI中如何給指定日期添加標(biāo)記問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02vue實(shí)現(xiàn)excel文件的導(dǎo)入和讀取完整步驟
Vue的數(shù)據(jù)綁定功能非常強(qiáng)大,很適合用來(lái)讀取Excel內(nèi)容,這篇文章主要給大家介紹了關(guān)于vue實(shí)現(xiàn)excel文件的導(dǎo)入和讀取的相關(guān)資料,文中通過(guò)代碼示例介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10Vue實(shí)現(xiàn)簡(jiǎn)單選項(xiàng)卡功能
這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)簡(jiǎn)單選項(xiàng)卡功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03Vue3進(jìn)階主題Composition API使用詳解
這篇文章主要為大家介紹了Vue3進(jìn)階主題Composition API使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04Vue渲染器如何對(duì)節(jié)點(diǎn)進(jìn)行掛載和更新
這篇文章主要介紹了Vue 的渲染器是如何對(duì)節(jié)點(diǎn)進(jìn)行掛載和更新的,文中通過(guò)代碼示例給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-05-05vue3+elementPlus項(xiàng)目支持生成、設(shè)置默認(rèn)附件方式
這篇文章主要介紹了vue3+elementPlus項(xiàng)目支持生成、設(shè)置默認(rèn)附件方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03