I recently implemented an axios interceptor in my App.vue file to handle all status 401 errors, allowing me to log out users. However, when trying to redirect to the login page, I keep encountering an error message saying "Cannot read property '$router' of undefined". I am unsure of what is causing this issue. Can anyone offer assistance? Additionally, I am curious if there is a more effective method for globally handling axios HTTP status errors, instead of having to check for statuses like 400 or 401 in each component that makes an HTTP request.
created: function () {
axios.interceptors.response.use(undefined, function (err) {
return new Promise(function (resolve, reject) {
if (err.response.status === 401 ) {
this.$router.push({ name: 'loginview' })
}
throw err;
});
});
}