在vue.js中进行两种类型的路由参数
我想在产品随后将URL放在嵌套的类别集中。不确定如何正确匹配路线。
我想拥有的URL:
--- renders a "category.show.vue": /$categorySlug+
app.com/catA/catB/
app.com/catA/catB/catXXX
--- renders a "product.show.vue": /$categorySlug+/$id-$productSlug
app.com/catA/111-nvidia-rtx-3080ti
app.com/catA/catB/222-nvidia-rtx-2060
app.com/catC/catD/333-iphone-13-pro
我尝试了这个,但它给了我错误:
{
path: '/:slug(\\w+)+/:productSlug(\\d+)',
name: 'product.show',
props: route => ({ slug: route.params.slug }),
component: () => import('pages/product.show.vue')
},
{
path: '/:slug+',
name: 'category.show',
props: route => ({ slug: route.params.slug }),
component: () => import('pages/category.show.vue')
}
有没有办法使它一起工作?
I would like to have URL in nested sets of categories following by a product. Not sure how to make a route matching correctly.
URLs I would like to have:
--- renders a "category.show.vue": /$categorySlug+
app.com/catA/catB/
app.com/catA/catB/catXXX
--- renders a "product.show.vue": /$categorySlug+/$id-$productSlug
app.com/catA/111-nvidia-rtx-3080ti
app.com/catA/catB/222-nvidia-rtx-2060
app.com/catC/catD/333-iphone-13-pro
I tried this but it's gives me the errors:
{
path: '/:slug(\\w+)+/:productSlug(\\d+)',
name: 'product.show',
props: route => ({ slug: route.params.slug }),
component: () => import('pages/product.show.vue')
},
{
path: '/:slug+',
name: 'category.show',
props: route => ({ slug: route.params.slug }),
component: () => import('pages/category.show.vue')
}
Is there a way to make it work together ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
中的REGEX模式:productsLug(\\ d+)
(即,\ d+
)仅匹配数字,但是您的产品URL实际上不仅包含数字(例如,之一,它们以/111-NVIDIA-RTX-3080TI
结束,因此不发生匹配。参数的模式必须与完全匹配。要解决问题,请在
productslug
模式中添加一个通配符匹配器:另外,每个URL中似乎只有一个产品,因此路由参数的模式不应附上
> +
:product.show
的最终路由路径应为:demo
The regex pattern in
:productSlug(\\d+)
(i.e.,\d+
) matches only digits, but your product URLs actually contain more than just digits (e.g., one of them ends with/111-nvidia-rtx-3080ti
), so no match occurs. The param's pattern must match exactly.To resolve the issue, add a wildcard matcher to the
productSlug
pattern:Also, it looks like there is only one product in each URL, so the pattern for the route param should not be appended with
+
:The final route path for
product.show
should be:demo