Home > Web Front-end > JS Tutorial > body text

How to implement data request display loading graph in vue2

亚连
Release: 2018-06-23 17:58:23
Original
7342 people have browsed it

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:





Copy after login

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
 }
}
Copy after login

The loading component is as follows:





Copy after login

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);
 }
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!