Handling 404 on axios call in mounted What would be the best way to handle 404 with vue router on axios callin the mounted of the component loaded with vue router?
Shall I simply redirect in catch statement?
Depends upon the context really. I've always liked axios interceptors, as you can cool things like:
axios.interceptors.response.use((response) => {
return response;
}, function (error) {
// Do something with response error
if (error.response.status === 401) {
console.log('unauthorized, logging out ...');
auth.logout();
router.replace('/auth/login');
}
return Promise.reject(error.response);
});
tried to put this in app.js
window.axios.interceptors.response.use((response) => {
return response;
}, function (error) {
if (error.response.status === 404) {
console.log('error')
this.$router.push({
name: '404'
});
}
return Promise.reject(error.response);
});
it logs to console however does not redirect, highly likely I set up router wrong way here.
Please sign in or create an account to participate in this conversation.