Home > Article > Web Front-end > How to implement data request display loading graph in vue2
This article mainly introduces vue2 to implement data request display loading diagram in detail, which has certain reference value. Interested friends can refer to it
In general projects, sometimes it is required, You display a gif image during data request, and then disappear after the data is loaded. For this, you generally only need to write js events in the encapsulated axios. Of course, we first need to add this image to app.vue. As follows:
<template> <p id="app"> <loading v-show="fetchLoading"></loading> <router-view></router-view> </p> </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>
The fetchLoading here is a variable stored in vuex. The following definition is required in store/modules/common.js:
/* 此js文件用于存储公用的vuex状态 */ import api from './../../fetch/api' import * as types from './../types.js' const state = { // 请求数据时加载状态loading fetchLoading: false } const getters = { // 请求数据时加载状态 fetchLoading: state => state.fetchLoading } const actions = { // 请求数据时状态loading FETCH_LOADING({ commit }, res) { commit(types.FETCH_LOADING, res) }, } const mutations = { // 请求数据时loading [types.FETCH_LOADING] (state, res) { state.fetchLoading = res } }
The loading component is as follows:
<template> <p class="loading"> <img src="./../../assets/main/running.gif" alt=""> </p> </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>
Finally, write the judgment loading event in the axios encapsulated in fetch/api.js : As follows
// axios的请求时间 let axiosDate = new Date() export function fetch (url, params) { return new Promise((resolve, reject) => { axios.post(url, params) .then(response => { // 关闭 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) => { // 关闭 loading图片消失 store.dispatch('FETCH_LOADING', false) axiosDate = new Date() reject(error) }) }) } export default { // 组件中公共页面请求函数 commonApi (url, params) { if(stringQuery(window.location.href)) { store.dispatch('FETCH_LOADING', true); } axiosDate = new Date(); return fetch(url, params); } }
This is achieved. When the data is loaded in the project, the gif image is displayed and disappears when the data is loaded.
Regarding vue.js learning tutorials, please click on the special vue.js component learning tutorials and Vue.js front-end component learning tutorials to learn.
For more vue learning tutorials, please read the special topic "Vue Practical Tutorial"
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
Using Angular5 to implement server-side rendering practice
How to reset the idle state in vuex
Use jQuery to encapsulate animate.css (detailed tutorial)
vue-cli configuration file (detailed tutorial)
The above is the detailed content of How to implement data request display loading graph in vue2. For more information, please follow other related articles on the PHP Chinese website!