我有一个这样的路由器:
routes: [
{
path: '/survey/:surveyHash',
name: 'survey',
component: Survey,
props: true,
},在组件中,我试图在道具中获得surveyHash。麻烦的是,当/在surveyHash中有斜杠时,我不能这么做。
vue-路由器有解决方案吗?
发布于 2018-12-21 20:45:05
您可以在路由定义中使用自定义匹配模式。
routes: [
{
path: '/survey/:surveyHash(.*)',
name: 'survey',
component: Survey,
props: true,
},这样,您将得到URL的其余部分,包括surveyHash中的斜杠。
发布于 2019-08-31 23:09:34
有更优雅的解决方案。Vue路由器在外壳下使用路径到regexp模块,并且它有现成的解决方案。
const regexp = pathToRegexp('/:foo*')
// keys = [{ name: 'foo', delimiter: '/', optional: true, repeat: true }]https://github.com/pillarjs/path-to-regexp#zero-or-more
const regexp = pathToRegexp('/:foo+')
// keys = [{ name: 'foo', delimiter: '/', optional: false, repeat: true }]https://stackoverflow.com/questions/53890365
复制相似问题