javascript - NuxtJS 和多级人类可读过滤器

标签 javascript seo nuxt.js

大家好!

我面临着一个我找不到解决方法的大问题。

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

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

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

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

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

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

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

有几个这样的部分,所以我设置了@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" 这样的值p>

有关详细信息,请参阅 documentation on vue router以及在 path-to-regexp 下使用的库的文档

关于javascript - NuxtJS 和多级人类可读过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66723441/

相关文章:

vue.js - @nuxt/auth 我可以使用参数发送刷新 token 吗?

javascript - 带下划线的回调函数_.each()

javascript - 如何最轻松地在 JQuery Mobile (JQM) 嵌入式/内部页面之间传递 URL 参数/数据?

seo - XML 站点地图会影响我的网站的 PCI 合规性吗?

apache - 如何将wordpress网站IP地址重定向到主页

javascript - 如何使 ng-repeat 内的链接可被 SEO 索引?

javascript - Nuxt Js asyncData 返回值表现得很奇怪。

javascript - ReCAPTCHA 占位符元素必须是元素或 id

javascript - 日期对象 Javascript 在使用毫秒时创建错误的时间

nuxt.js - 在nuxt中使用自定义布局更改页面时如何修复 `Invalid component name`?