我正试图重定向到404.使用router.beforeEach
全局钩子找不到页面上的html,但没有多大成功(使用Vue
1和Vue-Router
1.0):
router.beforeEach(function (transition) {
if (transition.to.path === '/*') {
window.location.href = '/404.html'
} else {
transition.next()
}
});
当插入不存在的路由时,这不是重定向到第404.html
页,只是给我一个空片段.只有当硬编码一些网站.com/*将重定向到预期的某个站点.com/404.html
我肯定我忽略了一些非常明显的东西,但我不知道是什么.
请注意,我要找的是redirect to a new page, not a redirect to another route,这可以通过使用router.redirect
轻松实现,比如以下代码片段:
router.redirect({
'*': '404'
})
而在我router.map
岁的时候,我可以有以下几点:
router.map({
'/404': {
name: '404'
component: {
template: '<p>Page Not Found</p>'
}
}
})