Home > Web Front-end > JS Tutorial > How to implement data request display loading graph in vue2

How to implement data request display loading graph in vue2

亚连
Release: 2018-06-23 17:58:23
Original
7615 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:

<template>
 <p id="app">
 <loading v-show="fetchLoading"></loading>
 <router-view></router-view>
 </p>
</template>

<script>
 import { mapGetters } from &#39;vuex&#39;;
 import Loading from &#39;./components/common/loading&#39;;

 export default {
 name: &#39;app&#39;,
 data() {
 return {
 }
 },
 computed: {
 ...mapGetters([
 &#39;fetchLoading&#39;,
 ]),
 },
 components: {
 Loading,
 }
 }
</script>

<style>
 #app{
 width: 100%;
 height: 100%;
 }
</style>
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 &#39;./../../fetch/api&#39;
import * as types from &#39;./../types.js&#39;
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:

<template>
 <p class="loading">
 <img src="./../../assets/main/running.gif" alt="">
 </p>
</template>

<script>
 export default {
 name: &#39;loading&#39;,
 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>
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(&#39;FETCH_LOADING&#39;, false)
 }, time)
 resolve(response.data)
 })
 .catch((error) => {
 // 关闭 loading图片消失
 store.dispatch(&#39;FETCH_LOADING&#39;, false)
 axiosDate = new Date()
 reject(error)
 })
 })
}
export default {
 // 组件中公共页面请求函数
 commonApi (url, params) {
 if(stringQuery(window.location.href)) {
 store.dispatch(&#39;FETCH_LOADING&#39;, 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