I'm trying to redirect to 404.html on page not found using the router.beforeEach
global hook, without much success (using Vue
and Vue-Router
1.0):
router.beforeEach(function (transition) {
if (transition.to.path === '/*') {
window.location.href = '/404.html'
} else {
transition.next()
}
});
This is not redirecting to page 404.html
when a non-existing route is inserted, just giving me an empty fragment. Only when hard-coding some-site.com/* will it redirect to the expected some-site.com/404.html
I'm sure there's something very obvious here that I'm overlooking, but I cannot figure out what.
Please note that what I am looking for is a redirect to a new page, not a redirect to another route, which could be easily achieved by using router.redirect
such as in these snippets:
router.redirect({
'*': '404'
})
Whereas on my router.map
, I could have the following:
router.map({
'/404': {
name: '404'
component: {
template: '<p>Page Not Found</p>'
}
}
})
<a>
tag that is not handled by Vue router? Does it navigate out of the app? – Gahnite<base href="/">
. That was unrelated to your question anyways. I have posted my thoughts as an answer below, please try it out and let me know if it works! – Gahnite