Last active
January 7, 2020 15:15
-
-
Save saninmersion/80171a6439fe041dc570ff76d2aa2f93 to your computer and use it in GitHub Desktop.
Axios Interceptor in vue js main app component
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
new Vue({ | |
el: "#app", | |
mounted() { | |
this.enableInterceptor() | |
}, | |
data: { | |
isLoading: false, | |
axiosInterceptor: null, | |
}, | |
methods: { | |
enableInterceptor() { | |
this.axiosInterceptor = window.axios.interceptors.request.use((config) => { | |
this.isLoading = true | |
return config | |
}, (error) => { | |
this.isLoading = false | |
return Promise.reject(error) | |
}) | |
window.axios.interceptors.response.use((response) => { | |
this.isLoading = false | |
return response | |
}, function(error) { | |
this.isLoading = false | |
return Promise.reject(error) | |
}) | |
}, | |
disableInterceptor() { | |
window.axios.interceptors.request.eject(this.axiosInterceptor) | |
}, | |
}, | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment