vue2實(shí)現(xiàn)數(shù)據(jù)請(qǐng)求顯示loading圖
一般項(xiàng)目中,有時(shí)候會(huì)要求,你在數(shù)據(jù)請(qǐng)求的時(shí)候顯示一張gif圖片,然后數(shù)據(jù)加載完后,消失。這個(gè),一般只需要在封裝的axios中寫(xiě)入js事件即可。當(dāng)然,我們首先需要在app.vue中,加入此圖片。如下:
<template>
<div id="app">
<loading v-show="fetchLoading"></loading>
<router-view></router-view>
</div>
</template>
<script>
import { mapGetters } from 'vuex';
import Loading from './components/common/loading';
export default {
name: 'app',
data() {
return {
}
},
computed: {
...mapGetters([
'fetchLoading',
]),
},
components: {
Loading,
}
}
</script>
<style>
#app{
width: 100%;
height: 100%;
}
</style>
這里的fetchLoading是存在vuex里面的一個(gè)變量。在store/modules/common.js里需要如下定義:
/* 此js文件用于存儲(chǔ)公用的vuex狀態(tài) */
import api from './../../fetch/api'
import * as types from './../types.js'
const state = {
// 請(qǐng)求數(shù)據(jù)時(shí)加載狀態(tài)loading
fetchLoading: false
}
const getters = {
// 請(qǐng)求數(shù)據(jù)時(shí)加載狀態(tài)
fetchLoading: state => state.fetchLoading
}
const actions = {
// 請(qǐng)求數(shù)據(jù)時(shí)狀態(tài)loading
FETCH_LOADING({
commit
}, res) {
commit(types.FETCH_LOADING, res)
},
}
const mutations = {
// 請(qǐng)求數(shù)據(jù)時(shí)loading
[types.FETCH_LOADING] (state, res) {
state.fetchLoading = res
}
}
loading組件如下:
<template>
<div class="loading">
<img src="./../../assets/main/running.gif" alt="">
</div>
</template>
<script>
export default {
name: 'loading',
data () {
return {}
},
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.loading{
position: fixed;
top:0;
left:0;
z-index:121;
width: 100%;
height: 100%;
background: rgba(0,0,0,0.3);
display: table-cell;
vertical-align: middle;
text-align: center;
}
.loading img{
margin:5rem auto;
}
</style>
最后在fetch/api.js里封裝的axios里寫(xiě)入判斷l(xiāng)oading事件即可:如下
// axios的請(qǐng)求時(shí)間
let axiosDate = new Date()
export function fetch (url, params) {
return new Promise((resolve, reject) => {
axios.post(url, params)
.then(response => {
// 關(guān)閉 loading圖片消失
let oDate = new Date()
let time = oDate.getTime() - axiosDate.getTime()
if (time < 500) time = 500
setTimeout(() => {
store.dispatch('FETCH_LOADING', false)
}, time)
resolve(response.data)
})
.catch((error) => {
// 關(guān)閉 loading圖片消失
store.dispatch('FETCH_LOADING', false)
axiosDate = new Date()
reject(error)
})
})
}
export default {
// 組件中公共頁(yè)面請(qǐng)求函數(shù)
commonApi (url, params) {
if(stringQuery(window.location.href)) {
store.dispatch('FETCH_LOADING', true);
}
axiosDate = new Date();
return fetch(url, params);
}
}
這樣就實(shí)現(xiàn)了,項(xiàng)目中當(dāng)加載數(shù)據(jù)的時(shí)候,顯示gif圖片,當(dāng)數(shù)據(jù)加載出來(lái)時(shí)消失。
關(guān)于vue.js的學(xué)習(xí)教程,請(qǐng)大家點(diǎn)擊專(zhuān)題vue.js組件學(xué)習(xí)教程、Vue.js前端組件學(xué)習(xí)教程進(jìn)行學(xué)習(xí)。
更多vue學(xué)習(xí)教程請(qǐng)閱讀專(zhuān)題《vue實(shí)戰(zhàn)教程》
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vue如何從接口請(qǐng)求數(shù)據(jù)
- vue.js實(shí)現(xiàn)請(qǐng)求數(shù)據(jù)的方法示例
- vuejs前后端數(shù)據(jù)交互之從后端請(qǐng)求數(shù)據(jù)的實(shí)例
- Vue2學(xué)習(xí)筆記之請(qǐng)求數(shù)據(jù)交互vue-resource
- vue請(qǐng)求數(shù)據(jù)的三種方式
- vue 請(qǐng)求后臺(tái)數(shù)據(jù)的實(shí)例代碼
- vue中promise的使用及異步請(qǐng)求數(shù)據(jù)的方法
- vue中實(shí)現(xiàn)先請(qǐng)求數(shù)據(jù)再渲染dom分享
- 談一談vue請(qǐng)求數(shù)據(jù)放在created好還是mounted里好
- Vue.js+HighCharts實(shí)現(xiàn)動(dòng)態(tài)請(qǐng)求展示時(shí)序數(shù)據(jù)
相關(guān)文章
Vue2.0+Vux搭建一個(gè)完整的移動(dòng)webApp項(xiàng)目的示例
這篇文章主要介紹了Vue2.0+Vux搭建一個(gè)完整的移動(dòng)webApp項(xiàng)目的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-03-03
Vue.js每天必學(xué)之?dāng)?shù)據(jù)雙向綁定
Vue.js每天必學(xué)之?dāng)?shù)據(jù)雙向綁定,如何進(jìn)行綁定,如何進(jìn)行數(shù)據(jù)雙向綁定,感興趣的小伙伴們可以參考一下2016-09-09
Vue3使用video-player實(shí)現(xiàn)視頻播放
video-player是一個(gè)基于video.js的視頻播放器組件,本文主要介紹了Vue3使用video-player實(shí)現(xiàn)視頻播放,具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01
vue項(xiàng)目無(wú)法刪除的問(wèn)題及解決
這篇文章主要介紹了vue項(xiàng)目無(wú)法刪除的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)的大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
Intellij IDEA搭建vue-cli項(xiàng)目的方法步驟
這篇文章主要介紹了Intellij IDEA搭建vue-cli項(xiàng)目的方法步驟,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
關(guān)于Vue中的計(jì)算屬性和監(jiān)聽(tīng)屬性詳解
這篇文章主要介紹了關(guān)于Vue中的計(jì)算屬性和監(jiān)聽(tīng)屬性詳解,Vue.js模板內(nèi)的表達(dá)式非常便利,但是設(shè)計(jì)它們的初衷是用于簡(jiǎn)單運(yùn)算的,在模板內(nèi)放入過(guò)長(zhǎng)的或復(fù)雜的邏輯時(shí),會(huì)讓模板過(guò)重且難以維護(hù),需要的朋友可以參考下2023-05-05
Vue3中數(shù)據(jù)響應(yīng)式原理與高效數(shù)據(jù)操作全解析
這篇文章主要為大家詳細(xì)介紹了Vue3中數(shù)據(jù)響應(yīng)式原理與高效數(shù)據(jù)操作的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-02-02

