vue2實(shí)現(xiàn)數(shù)據(jù)請(qǐng)求顯示loading圖
一般項(xiàng)目中,有時(shí)候會(huì)要求,你在數(shù)據(jù)請(qǐng)求的時(shí)候顯示一張gif圖片,然后數(shù)據(jù)加載完后,消失。這個(gè),一般只需要在封裝的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里面的一個(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里寫入判斷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)擊專題vue.js組件學(xué)習(xí)教程、Vue.js前端組件學(xué)習(xí)教程進(jìn)行學(xué)習(xí)。
更多vue學(xué)習(xí)教程請(qǐng)閱讀專題《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)文章
vue插槽slot的簡(jiǎn)單理解與用法實(shí)例分析
這篇文章主要介紹了vue插槽slot的簡(jiǎn)單理解與用法,結(jié)合實(shí)例形式分析了vue插槽slot的功能、原理、相關(guān)使用技巧與操作注意事項(xiàng),需要的朋友可以參考下2020-03-03Vite多環(huán)境配置及變量識(shí)別規(guī)則
這篇文章主要為大家介紹了Vite多環(huán)境配置時(shí)間及vite識(shí)別環(huán)境變量的規(guī)則,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09vue中el-table實(shí)現(xiàn)自動(dòng)吸頂效果(支持fixed)
本文主要介紹了vue中el-table實(shí)現(xiàn)自動(dòng)吸頂效果,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09vue style width a href動(dòng)態(tài)拼接問(wèn)題的解決
這篇文章主要介紹了vue style width a href動(dòng)態(tài)拼接問(wèn)題的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08Vue 組件參數(shù)校驗(yàn)與非props特性的方法
這篇文章主要介紹了Vue 組件參數(shù)校驗(yàn)與非props特性的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-02-02vue-cli 3.x 配置Axios(proxyTable)跨域代理方法
今天小編就為大家分享一篇vue-cli 3.x 配置Axios(proxyTable)跨域代理方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09Vue函數(shù)式組件的應(yīng)用實(shí)例詳解
這篇文章主要介紹了Vue函數(shù)式組件的應(yīng)用實(shí)例詳解,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08