Vue3路由器-如何在子路由中使用beforeRouteEnter?
我想在根路由上有一个可选组件,它将根据路由参数中提供的 id 显示详细信息页面。如果 id 参数不对应于任何对象,我希望将用户重定向到根路由。我相信 beforeRouteEnter() 可能是实现此目的的好方法,但是它似乎没有在以下代码片段中被调用:
{
path: '/',
name: 'Home',
component: Home,
children: [
{
path: '/:id(\\d+)',
name: 'Detail',
component: Detail,
beforeRouteEnter(to, from, next) {
alert("beforeRouteEnter in child!")
}
}]
}
我的 beforeRouteEnter 实现中的错误在哪里?或者我应该使用完全不同的东西来实现我想要的?谢谢。
I would like to have an optional component on the root route which would be display detail page based on the id provided in the route params. In case when the id param does not correspond to any object, I wish to redirect the user to the root route. I believe beforeRouteEnter() could be a nice way to achieve this, however it does not seem to get called in the following code snippet:
{
path: '/',
name: 'Home',
component: Home,
children: [
{
path: '/:id(\\d+)',
name: 'Detail',
component: Detail,
beforeRouteEnter(to, from, next) {
alert("beforeRouteEnter in child!")
}
}]
}
Where is the mistake in my beforeRouteEnter implementation? Or should I use something completely different to achieve what I want? Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您应该使用
beforeEnter
。beforeRouteEnter
是一个组件方法。您可以在此处查看它们的使用位置< /a>.
The problem is that you should be using
beforeEnter
.beforeRouteEnter
is a component method.You can check out where to use each of them here.