NuxtJS 和多级人类可读过滤器

NuxtJS and multilevel human-readable filters

大家好!

我遇到了一个大问题,但找不到解决方法。

简而言之 - 我希望能够生成以前未知长度的多级路由来创建过滤器。

更多详情 有一个网站site.com

它有部分:site.com/newssite.com/articles等等。为简单起见,我将它们表示为 site.com/section

每个部分都有类别:site.com/news/peoplesite.com/articles/how。为简单起见,我将它们表示为 site.com/section/category

栏目和类别的数量可以超过一个

当用户转到某个部分时,他们会看到该部分中的完整帖子列表。

当用户转到该版块的类别时,他只能看到该类别中的新闻。

有几个这样的部分,所以我设置@nuxt/routejs禁用基于pages目录的路由。 route.js 看起来像这样

Vue.use(Router)

export function createRouter() {
  return new Router({
    mode: 'history',
    routes: [
        // Home page of the site
      {
        path: '/',
        component: BasePage,
      },
      // Page with a section
      {
        name: 'section',
        path: '/:section',
        component: PostBase,
      },
      // Also a page with a section, but with pagination taken into account
      {
        name: 'section-page',
        path: '/:section/page/:page',
        component: PostBase,
      },
      // Category page inside the section
      {
        name: 'category',
        path: '/:section/:category',
        component: PostBase,
      },
      // Category page inside the section, but with pagination taken into account
      {
        name: 'category-page',
        path: '/:section/:category/page/:page',
        component: PostBase,
      },
    ]
  })
}

然后我不知道如何正确解决问题。 我想让用户能够使用尽可能接近 SEO 推荐的“智能过滤器”。 例如,它可以使用单个过滤器并得到:

site.com/news/sorting-date

并且可以获得:

site.con/news/sorting-date/author-elisa/page/4

或者是:

site.com/news/people/sorting-views/city-new-york/page/2

我可以举更多的例子,但我认为从这三个例子中可以清楚地看出,如果过滤器数量未知(我通过管理面板设置),可能会有未知数量的斜杠。

我该怎么办?如何正确解决这个问题?

我也为非常糟糕的英语道歉。我真的希望得到你的帮助甚至提示:)

您可以使用“一个或多个”或“零个或多个”匹配器执行此操作:

{
  name: 'category',
  path: '/:section/:category/:filters*',  // or :filters+ if you want at least one
  component: PostBase,
},

那么您将在 this.$route.params.filters 下拥有像 "sorting-views/city-new-york/page/2"

这样的值

有关详细信息,请参阅 documentation on vue router and also the documentation of the library used underneath path-to-regexp