vue ajax封装loading extend store

通过vuex 进行loading数据管理

store

// 全局变量
const state = {
  // 页面加载时
  pageLoading: false,
  // 数据加载时
  dataLoading: false,

  // 音频图标
  calendaricon: 0,

};
// computed作用
const getters = {
  calendaricon: (state) => {
    return state.calendaricon;
  }
};
// 数据处理
const mutations = {
  // 数据加载时
  setDataLoading(state, payload) {
    state.dataLoading = payload;
  },
  }
  // 异步处理
import Axios from "axios";
const actions = {
  message() {
    return Axios.request({
      url: "/ajax/user/wx/information/detail",
      method: "get",
      params: {
        contentId: 408,
      },
    }).then((res) => {
      return Promise.resolve(res);
    });
  },
  }
 
 export default new Vuex.Store({
  // strict: process.env.NODE_ENV !== 'production',
  state,
  getters,
  mutations,
  actions,
});

app.vue

<template>
  <div>
    <keep-alive :include="routerlist">
      <router-view></router-view>
    </keep-alive>
    <loading v-model="$store.state.dataLoading"></loading>
  </div>
</template>
import loading from "@/components/common/loading";
export default {
  data() {
    return {
      routerlist: [],
    };
  },
   components: {
    loading,
  },

axios

import store from "store";

let num = 0;
// 添加请求拦截器
axios.interceptors.request.use(function (config) {
    num++;
    if(nmu==1){
      store.commit("setDataLoading", true);
    }

   
    // 在发送请求之前做些什么
    return config;
  }, function (error) {
    num--
    // 对请求错误做些什么
    return Promise.reject(error);
  });

// 添加响应拦截器
axios.interceptors.response.use(function (response) {
    // 2xx 范围内的状态码都会触发该函数。
    // 对响应数据做点什么
     num--;
    if (num == 0) {
      store.commit("setDataLoading", false);
    }
    return response;
  }, function (error) {
    // 超出 2xx 范围的状态码都会触发该函数。
    // 对响应错误做点什么
     num--;
    if (num == 0) {
      store.commit("setDataLoading", false);
    }
    return Promise.reject(error);
  });
  // 挂载原型|分布式描写都可以
  Vue.prototype.$http = Axios;
  Axios.post().then(res=>{})

vue extend 动态添加

! 注意:extend 新增的组件没有 router store 需要动态注册才行

//     import store from "store";
//     let data={
//       isload:true
//        }
//     let instance = new loading({
//      data,this.$router,store
//     }).$mount();
import load from '@/common/loading/loading'
const loading = Vue.extend(load)
let instance = new loading().$mount();
document.body.appendChild(instance.$el)
     
let num = 0;
// 添加请求拦截器
axios.interceptors.request.use(function (config) {
    num++;
    if(nmu==1){
       Vue.nextTick(() => {
          instance.isload = true
          // show 和弹窗组件里的show对应,用于控制显隐
        })
    }

   
    // 在发送请求之前做些什么
    return config;
  }, function (error) {
    num--
    // 对请求错误做些什么
    return Promise.reject(error);
  });

// 添加响应拦截器
axios.interceptors.response.use(function (response) {
    // 2xx 范围内的状态码都会触发该函数。
    // 对响应数据做点什么
     num--;
    if (num == 0) {
       Vue.nextTick(() => {
          instance.isload = true
          // show 和弹窗组件里的show对应,用于控制显隐
        })
    }
    return response;
  }, function (error) {
    // 超出 2xx 范围的状态码都会触发该函数。
    // 对响应错误做点什么
     num--;
    if (num == 0) {
        Vue.nextTick(() => {
          instance.isload = true
          // show 和弹窗组件里的show对应,用于控制显隐
        })
    }
    return Promise.reject(error);
  });
  // 挂载原型|分布式描写都可以
  Vue.prototype.$http = Axios;
  Axios.post().then(res=>{})
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Vue2中封装loading可以通过自定义指令或者组件来实现。下面分别介绍这两种方式: 1. 自定义指令: ```javascript // main.js Vue.directive('loading', { bind: function (el, binding) { const loadingText = document.createElement('span'); loadingText.className = 'loading-text'; loadingText.innerHTML = 'Loading...'; const loadingSpinner = document.createElement('div'); loadingSpinner.className = 'loading-spinner'; const loadingContainer = document.createElement('div'); loadingContainer.className = 'loading-container'; loadingContainer.appendChild(loadingText); loadingContainer.appendChild(loadingSpinner); el.appendChild(loadingContainer); if (binding.value) { el.classList.add('loading'); } }, update: function (el, binding) { if (binding.value) { el.classList.add('loading'); } else { el.classList.remove('loading'); } } }); // 使用指令 <template> <div v-loading="isLoading"> <!-- 内容 --> </div> </template> <script> export default { data() { return { isLoading: false }; }, methods: { fetchData() { this.isLoading = true; // 发起请求 // 请求结束后将isLoading设置为false } } } </script> ``` 2. 封装组件: ```vue <template> <div class="loading-container" v-if="isLoading"> <span class="loading-text">Loading...</span> <div class="loading-spinner"></div> </div> <div v-else> <!-- 内容 --> </div> </template> <script> export default { props: { isLoading: { type: Boolean, default: false } } } </script> <style scoped> .loading-container { position: relative; } .loading-text { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); } .loading-spinner { /* 样式自定义 */ } </style> ``` 使用组件: ```vue <template> <div> <loading :is-loading="isLoading"></loading> <!-- 内容 --> </div> </template> <script> import Loading from './Loading.vue'; export default { components: { Loading }, data() { return { isLoading: false }; }, methods: { fetchData() { this.isLoading = true; // 发起请求 // 请求结束后将isLoading设置为false } } } </script> ``` 以上就是Vue2中封装loading的两种方式,你可以根据自己的需求选择适合的方式来实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

web修理工

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值