我是vuejs的新手,我正在和vue一起做我的第一个项目.我只是想知道,如果找不到请求的url,我将如何路由到我的404.vue
组件.
知道吗?
我是vuejs的新手,我正在和vue一起做我的第一个项目.我只是想知道,如果找不到请求的url,我将如何路由到我的404.vue
组件.
知道吗?
在路由声明中,我想补充以下内容:
[
...
{ path: '/404', component: NotFound },
{ path: '*', redirect: '/404' },
...
]
这意味着,如果用户被导航到与任何路由都不匹配的路径,它将被重定向到"404"路由,该路由将包含"not found"消息.
我将其分为两条路由的原因是,在您需要的某些数据无法解析的情况下,您还可以通过编程将用户 bootstrap 到404路由.
例如,如果你正在创建一个博客,你可能有以下途径:
{ path: '/posts/:slug', component: BlogPost }
这将解决问题,即使提供的slug实际上没有检索任何博客文章.要处理此问题,当应用程序确定找不到帖子时,请执行以下操作:
return this.$router.push('/404')
或
return router.push('/404')
如果您不在Vue组件的上下文中.
One thing to bear in mind though is that the c或rect way to handle a not found response isn't just to serve an err或 page - you should try to serve an actual HTTP 404 response to the browser. You won't need to do this if the user is already inside a single-page-application, but if the browser hits that example blog post as its initial request, the server should really return a 404 code.