admin管理员组文章数量:1202374
I am creating a VueJS app which heavily uses the vue router.
Here's the structure of my routes.js
:
export const routes = [
{ path: '', component: Poetry, children: [
{ path: '', name: 'poetry', component: PoetryLanding },
{ path: 'poetry/:id', name: 'poetrycard', component: PoetryCard },
{ path: 'poetry/search', name: 'poetrysearch', component: PoetrySearch },
]},
]
I want to use regex in second child component i.e. poetrycard
such that it only accepts the params(:id
) which is a number. I am doing this because poetrysearch
gets affected as /search
is treated as :id
!
I tried:
{ path: 'poetry/:id/[0-9]/g', name: 'poetrycard', component: PoetryCard }
But this doesn't work. Please help.
I am creating a VueJS app which heavily uses the vue router.
Here's the structure of my routes.js
:
export const routes = [
{ path: '', component: Poetry, children: [
{ path: '', name: 'poetry', component: PoetryLanding },
{ path: 'poetry/:id', name: 'poetrycard', component: PoetryCard },
{ path: 'poetry/search', name: 'poetrysearch', component: PoetrySearch },
]},
]
I want to use regex in second child component i.e. poetrycard
such that it only accepts the params(:id
) which is a number. I am doing this because poetrysearch
gets affected as /search
is treated as :id
!
I tried:
{ path: 'poetry/:id/[0-9]/g', name: 'poetrycard', component: PoetryCard }
But this doesn't work. Please help.
Share Improve this question edited Jul 12, 2018 at 9:57 Axel asked Jul 12, 2018 at 9:48 AxelAxel 5,11118 gold badges75 silver badges146 bronze badges1 Answer
Reset to default 23vue-router uses path-to-regexp as its path matching engine, so it supports many advanced matching patterns such as optional dynamic segments, zero or more / one or more requirements, and even custom regex patterns.
So change your route definition as follows:
{ path: 'poetry/:id(\\d+)', name: 'poetrycard', component: PoetryCard }
Reference- Advanced Matching patterns
本文标签: javascriptUse Regex in Vue Router PathsStack Overflow
版权声明:本文标题:javascript - Use Regex in Vue Router Paths - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738606497a2102377.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论