先介绍第一种 基于饿了么组件中的全局ajax请求等待的书写方法
main.js中
import EventBus from './../src/core/EventBus';
Vue.use(EventBus);
Vue.http.interceptors.push((request, next) => {
// before sending request
EventBus.$emit('toggleLoading')
next((response => {
// after sending request
EventBus.$emit('toggleLoadingEnd')
}));
});
eventBus文件
import Vue from 'vue';
const EventBus = new Vue();
export default EventBus;
Vue文件中
import EventBus from './../core/EventBus’;
mounted() {
console.log("加载完成App.vue");
EventBus.$on('toggleLoading', () => {
this.$loading({ fullscreen: true })
});
EventBus.$on('toggleLoadingEnd', () => {
this.$loading({ fullscreen: true }).close();
});
}