首页 > 解决方案 > 如何使用正则表达式匹配nuxtjs,vue中路由中的特定单词?

问题描述

我有这条路线/test/a-vs-b

只有在其中找到这条路线时,我才试图抓住这条路线-vs-

我尝试了一些正则表达式变体,但似乎没有任何效果

routes.push({
    name: 'test',
    path: '/test/:page((.*)-vs-(.*))',
    component: resolve(__dirname, 'test/b.vue'),
});

有任何想法吗?

标签: javascriptvue.jsroutesnuxt.js

解决方案


VueRouter 使用该path-to-regexp库,它显然不能像您尝试做的那样处理用括号定义捕获组。


我通过简单地删除围绕.*s 的括号来使其工作。

routes.push({
    name: 'test',
    path: '/test/:page(.*-vs-.*)',
    component: resolve(__dirname, 'test/b.vue'),
});

推荐阅读