1. 定义根state:ajaxIsLoading
2. 在Axios拦截器中commit不同的状态实现状态切换
3. 组件中通过getter获取ajaxIsLoading状态

Axios 拦截器配置

import Vue from 'vue'
import Axios from 'axios'
import AppStore from '../store'
import * as types from '../store/mutation-types.js' Vue.prototype.$http = Axios Axios.interceptors.request.use(config => {
// console.log('ajax begin request')
AppStore.commit(types.AJAX_BEGIN_RQUEST)
return config;
}) Axios.interceptors.response.use(config => {
// console.log('ajax get response')
AppStore.commit(types.AJAX_END_REQUEST)
return config
})

Vuex

const state = {
ajaxIsLoading: false
} const mutations = {
['AJAX_BEGIN_REQUEST'](state) {
state.ajaxIsLoading = true
},
['AJAX_END_REQUEST'](state) {
state.ajaxIsLoading = false
}
} const getter = {
ajaxIsLoading: state => state.ajaxIsLoading
}

Loading.vue

<template>
<div id="loading-container" v-show="ajaxIsLoading">
<div id="loading" >
<img src="../assets/loading.gif" alt="loading">
</div>
</div>
</template>
<script>
import { mapGetters } from 'vuex'
export default {
name: 'loading',
computed: {
...mapGetters(['ajaxIsLoading']) }
}
</script>
<style>
#loading-container {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
opacity: .3;
background: #ccc;
z-index: 10000;
} #loading {
position: absolute;
left: 50%;
top: 50%;
width: 40px;
height: 40px;
z-index: 100001;
}
</style>

Vue2基于Axios Ajax Vuex的Loading组件的更多相关文章

  1. Vue基于vuex、axios拦截器实现loading效果及axios的安装配置

    准备 利用vue-cli脚手架创建项目 进入项目安装vuex.axios(npm install vuex,npm install axios) axios配置 项目中安装axios模块(npm in ...

  2. vue2.0+webpack+vuerouter+vuex+axios构建项目基础

    前言 本文讲解的是vue2.0+webpack+vuerouter+vuex+axios构建项目基础 步骤 1.全局安装webpack,命令 npm install webpack -g 注意,web ...

  3. day 87 Vue学习六之axios、vuex、脚手架中组件传值

      本节目录 一 axios的使用 二 vuex的使用 三 组件传值 四 xxx 五 xxx 六 xxx 七 xxx 八 xxx 一 axios的使用 Axios 是一个基于 promise 的 HT ...

  4. day 84 Vue学习六之axios、vuex、脚手架中组件传值

    Vue学习六之axios.vuex.脚手架中组件传值   本节目录 一 axios的使用 二 vuex的使用 三 组件传值 四 xxx 五 xxx 六 xxx 七 xxx 八 xxx 一 axios的 ...

  5. 基于vue2.0打造移动商城页面实践 vue实现商城购物车功能 基于Vue、Vuex、Vue-router实现的购物商城(原生切换动画)效果

    基于vue2.0打造移动商城页面实践 地址:https://www.jianshu.com/p/2129bc4d40e9 vue实现商城购物车功能 地址:http://www.jb51.net/art ...

  6. vue2.0 仿手机新闻站(五)全局的 loading 组件

    1.组件结构 index.js const LoadingComponent = require('./Loading.vue') const loading = { install: functio ...

  7. [Loading Component]Loading组件的v-model设计是否不合理?

    vue在2.4.2版本中给computed里的属性加了限制,详见assigning to a computed property without setter does not fail 项目将vue ...

  8. vue全局loading组件

    本组件作用在页面加载完成前进行loader提示,提升用户体验,只需要在app.vue中引用一次,整个项目中路由切换时就可以自动进行提示(vuex版): 1. 添加vuex值和方法: import Vu ...

  9. axios和vuex

    0.babel 将es6代码转换成各个浏览器都能识别的代码 一.axios 1.官方网站 https://www.kancloud.cn/yunye/axios/234845 2.引用: (1)cdn ...

随机推荐

  1. SQL 时间函数 Datepart()与DateName()

    1.Datepart() 返回代表指定日期的指定日期部分的整数 语法 Datepart(datepart,date) 返回类型 int datepart: 日期部分 缩写 year yy, yyyy ...

  2. 洛谷 P2073 送花

    这题其实可以用vector水掉! 定义: 记住要用结构体(c为价格,x为美丽值)! 以c排序. struct Node { int x,c; bool operator < (const &am ...

  3. Android 屏幕切换动画

    public void overridePendingTransition (int enterAnim, int exitAnim) Call immediately after one of th ...

  4. JSONObject和JSONArray 以及Mybatis传入Map类型参数

    import org.json.JSONArray;import org.json.JSONObject; 将字符串转化为JSONArray   JSONArray  jsonArray = new ...

  5. Pytorch(一)

    一.Pytorch介绍 Pytorch 是Torch在Python上的衍生物 和Tensorflow相比: Pytorch建立的神经网络是动态的,而Tensorflow建立的神经网络是静态的 Tens ...

  6. PHP逐字符读取数据

    <?php $file = fopen("Minot.txt", "r") or exit("Unable to open file!" ...

  7. java.util.Calendar简介

    Calendar是一个抽象类,我们无法直接实例化它,它有一个具体子类实体类java.util.GregorianCalendar,这个类实现的就是我们日常所用的公历历法,或者叫做阳历.我们可以直接使用 ...

  8. Android 6.0 Kotlin 蓝牙BLE扫描

    package com.arci.myapplication import android.os.Bundleimport android.support.design.widget.Snackbar ...

  9. MongoDB win32-bit 安装

    一念起: 由于本人 用的电脑比较老旧,所以一直用的 是win7 32bit 的操作系统,但是在学习MongoDB的时候 遇到了起步的第一个问题,按照目前 官网最新版MongoDB 3.4.3,已不支持 ...

  10. C# Json格式

    using LitJson; //自定义Json类 JsonDataResult jsondata = new JsonDataResult() { Success = false }; HttpCo ...