我正在尝试重定向到使用router.beforeEach全局挂钩未找到的页面上的404.html ,但没有太大成功(使用Vue和Vue-Router1.0):
router.beforeEach(function (transition) {
if (transition.to.path === '/*') {
window.location.href = '/404.html'
} else {
transition.next()
}
});
Run Code Online (Sandbox Code Playgroud)
404.html插入不存在的路径时,这不会重定向到页面,只是给我一个空片段.只有在硬编码some-site.com/*时,它才会重定向到预期的some-site.com/404.html
我确信这里有一些非常明显的东西,我忽略了,但我无法弄清楚是什么.
请注意,我正在寻找的是重定向到新页面,而不是重定向到另一个路由,这可以通过使用router.redirect这些片段轻松实现:
router.redirect({
'*': '404'
})
Run Code Online (Sandbox Code Playgroud)
在我身上router.map,我可以拥有以下内容:
router.map({
'/404': {
name: '404'
component: {
template: '<p>Page Not Found</p>'
}
}
})
Run Code Online (Sandbox Code Playgroud)