Skip to content

Instantly share code, notes, and snippets.

@saninmersion
Last active January 7, 2020 15:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saninmersion/80171a6439fe041dc570ff76d2aa2f93 to your computer and use it in GitHub Desktop.
Save saninmersion/80171a6439fe041dc570ff76d2aa2f93 to your computer and use it in GitHub Desktop.
Axios Interceptor in vue js main app component
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