vue2實現(xiàn)數(shù)據(jù)請求顯示loading圖
一般項目中,有時候會要求,你在數(shù)據(jù)請求的時候顯示一張gif圖片,然后數(shù)據(jù)加載完后,消失。這個,一般只需要在封裝的axios中寫入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里面的一個變量。在store/modules/common.js里需要如下定義:
/* 此js文件用于存儲公用的vuex狀態(tài) */
import api from './../../fetch/api'
import * as types from './../types.js'
const state = {
// 請求數(shù)據(jù)時加載狀態(tài)loading
fetchLoading: false
}
const getters = {
// 請求數(shù)據(jù)時加載狀態(tài)
fetchLoading: state => state.fetchLoading
}
const actions = {
// 請求數(shù)據(jù)時狀態(tài)loading
FETCH_LOADING({
commit
}, res) {
commit(types.FETCH_LOADING, res)
},
}
const mutations = {
// 請求數(shù)據(jù)時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里寫入判斷l(xiāng)oading事件即可:如下
// axios的請求時間
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 {
// 組件中公共頁面請求函數(shù)
commonApi (url, params) {
if(stringQuery(window.location.href)) {
store.dispatch('FETCH_LOADING', true);
}
axiosDate = new Date();
return fetch(url, params);
}
}
這樣就實現(xiàn)了,項目中當(dāng)加載數(shù)據(jù)的時候,顯示gif圖片,當(dāng)數(shù)據(jù)加載出來時消失。
關(guān)于vue.js的學(xué)習(xí)教程,請大家點擊專題vue.js組件學(xué)習(xí)教程、Vue.js前端組件學(xué)習(xí)教程進行學(xué)習(xí)。
更多vue學(xué)習(xí)教程請閱讀專題《vue實戰(zhàn)教程》
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vue如何從接口請求數(shù)據(jù)
- vue.js實現(xiàn)請求數(shù)據(jù)的方法示例
- vuejs前后端數(shù)據(jù)交互之從后端請求數(shù)據(jù)的實例
- Vue2學(xué)習(xí)筆記之請求數(shù)據(jù)交互vue-resource
- vue請求數(shù)據(jù)的三種方式
- vue 請求后臺數(shù)據(jù)的實例代碼
- vue中promise的使用及異步請求數(shù)據(jù)的方法
- vue中實現(xiàn)先請求數(shù)據(jù)再渲染dom分享
- 談一談vue請求數(shù)據(jù)放在created好還是mounted里好
- Vue.js+HighCharts實現(xiàn)動態(tài)請求展示時序數(shù)據(jù)
相關(guān)文章
vue中el-table實現(xiàn)自動吸頂效果(支持fixed)
本文主要介紹了vue中el-table實現(xiàn)自動吸頂效果,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09
vue style width a href動態(tài)拼接問題的解決
這篇文章主要介紹了vue style width a href動態(tài)拼接問題的解決,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
vue-cli 3.x 配置Axios(proxyTable)跨域代理方法
今天小編就為大家分享一篇vue-cli 3.x 配置Axios(proxyTable)跨域代理方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09

