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就有點復(fù)雜了,欲知后事如何,且聽下回分解~
您可能感興趣的文章:
- 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如何解決數(shù)據(jù)加載時,插值表達式閃爍問題
這篇文章主要介紹了vue如何解決數(shù)據(jù)加載時,插值表達式閃爍問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01vue+element UI中如何給指定日期添加標(biāo)記
這篇文章主要介紹了vue+element UI中如何給指定日期添加標(biāo)記問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02vue實現(xiàn)excel文件的導(dǎo)入和讀取完整步驟
Vue的數(shù)據(jù)綁定功能非常強大,很適合用來讀取Excel內(nèi)容,這篇文章主要給大家介紹了關(guān)于vue實現(xiàn)excel文件的導(dǎo)入和讀取的相關(guān)資料,文中通過代碼示例介紹的非常詳細,需要的朋友可以參考下2023-10-10vue3+elementPlus項目支持生成、設(shè)置默認附件方式
這篇文章主要介紹了vue3+elementPlus項目支持生成、設(shè)置默認附件方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03