-
Notifications
You must be signed in to change notification settings - Fork 176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to use vue-spinner in interceptors of axios #19
Comments
I was going to ask something also, before I realized the maintainer of this project doesn't seem to have discovered the "issues" tab. Anyway, this is how I would use it: <pulse-loader :loading="loading" :color="color" :size="size"></pulse-loader> data () {
return {
loading : true
}
},
methods: {
getSomething () {
axios.get('/api').then(res => {
if(res.data) {
...
this.loading = false
}
}).catch(err => {
if(err.msg) {
...
this.loading = false
}
})
}
} |
@samayo I have been resolved this issues.My axios config file is a single '.js' ,not depend on '.vue' file. |
export const eventHub = new Vue();
import { eventHub } from 'eventhub'
function createAxios() {
const axios = Axios.create({ ... });
axios.interceptors.request.use(
conf => {
eventHub.$emit('before-request');
return conf;
},
error => {
eventHub.$emit('request-error');
return Promise.reject(error);
}
);
axios.interceptors.response.use(
response => {
eventHub.$emit('after-response');
return response;
},
error => {
eventHub.$emit('response-error');
return Promise.reject(error);
}
);
return axios;
}
import { eventHub } from 'utils/eventhub'
export default {
data () {
return { spinnerVisible: false }
},
methods: {
showSpinner() {
console.log('show spinner');
this.spinnerVisible = true;
},
hideSpinner() {
console.log('hide spinner');
this.spinnerVisible = false;
}
},
created() {
eventHub.$on('before-request', this.showSpinner);
eventHub.$on('request-error', this.hideSpinner);
eventHub.$on('after-response', this.hideSpinner);
eventHub.$on('response-error', this.hideSpinner);
},
beforeDestroy() {
eventHub.$off('before-request', this.showSpinner);
eventHub.$off('request-error', this.hideSpinner);
eventHub.$off('after-response', this.hideSpinner);
eventHub.$off('response-error', this.hideSpinner);
}
} But as @LJade said, it is better to use Vuex rather than the event bus. |
If you are using vue CLI: in your axios.interceptors.request.use(function (config) { Axios response interceptor axios.interceptors.response.use(function (config) { create a component xxx and register it.. in your App.vue Now this automatically displays loading symbol on every axios request |
Hello @mrts Thanks for the reply, the approach you explained in Vue 2 with event bus... can you please help me to implement same thing in Vue 3 with mitt library. |
I want to realize the loading shows when ajax request occurred but close when get ajax response. Vue-spinner is beautiful but I can't find the document to control the spinner switch.Also, options config is not friendly.Is there any solution? Thank you very much!
The text was updated successfully, but these errors were encountered: