【发布时间】:2019-12-14 07:48:00
【问题描述】:
如何使用守卫移动到下一条路由规则而不是重定向到特定路由?
例如,我有一些路线:
routes: [
{ path: '/', component: Main },
{ path: '/about', component: About },
{ path: '/:product', component: Product },
{ path: '*', component: NotFoundException }
]
在 Product 组件中我有 beforeRouteEnter 这样的功能:
beforeRouteEnter(to, from, next) {
const question = checkIfExist(to.params.product);
if (question) { next(); return;}
next();
}
如果产品不存在,那么它应该被重定向到 NotFoundException 组件。在 stackoverflow 示例中,我只看到重定向到特定 url(如登录)的示例,这没有帮助。
当我给出 url: '/some' 然后 vue 检查是否:
- 是/?没有 => 下一条路线。
- 是 /about 吗?没有 => 下一条路线。
- 是/:产品?没有 => 下一条路线。
- 显示 NotFoundException 组件。
【问题讨论】:
-
你真正想要达到什么目的? .请明确目标
-
如果产品不存在,那么它应该是'/',这是你的主组件
标签: vue.js vue-router