【问题标题】:Aurelia route matchingAurelia 路由匹配
【发布时间】:2017-12-26 13:18:02
【问题描述】:

注意:pushState 已启用

我有一个与 aurelia 路由匹配相关的问题。我在 app.ts 中有以下路线:

{
  route: ['profile'],
  href: '#',
  name: 'profile',
  moduleId: PLATFORM.moduleName('../pages/profile', 'profile'),
  title: 'Profile'
},
{
  route: [':continents/:countries'],
  name: 'countries',
  moduleId: PLATFORM.moduleName('../pages/countries', 'countries'),
  title: 'Countries'
}

profile.ts 模块中,我做了另一个 configRoute,我在其中设置了另外 2 个子路由,例如:

{
  route: ['personal'],
  href: '#',
  name: 'personal',
  moduleId: PLATFORM.moduleName('./personal', 'personal'),
  title: 'Personal'
},
{
  route: ['skills'],
  href: '#',
  name: 'skills',
  moduleId: PLATFORM.moduleName('./skills', 'skills'),
  title: 'Skills'
}

问题如下:当我导航到/profile/skills/profile/personal 时,路线将匹配国家一(':continents/:countries')。我认为这是有道理的,因为整个片段都符合这一点。

我可以将个人资料路线更改为

{
  route: ['profile', 'profile/:tabName'],
  href: '#',
  name: 'profile',
  moduleId: PLATFORM.moduleName('../pages/profile', 'profile'),
  title: 'Profile'
},

但是我的路线会像 /profile/personal/personal/profile/personal/skills 我不想要。

我已尝试按照 aurelia 文档中的建议添加通配符:

{
  route: ['profile', 'profile/*tabName'],
  href: '#',
  name: 'profile',
  moduleId: PLATFORM.moduleName('../pages/profile', 'profile'),
  title: 'Profile'
},

但是像/profile/skills 这样的路由不匹配它,它仍然去了':continents/:countries'

我可以强制它转到profile 页面吗? (除了子路由'countries'

【问题讨论】:

    标签: routing aurelia


    【解决方案1】:

    主应用程序路由器在尝试匹配路由字符串时获得第一个 dib,profile/skills 匹配 ':continents/:countries'

    ':continents/:countries' 相当于.+/.+ 作为路由正则表达式。它几乎可以匹配任何中间带有斜线的字符串。

    您需要在该路由中添加 'earth/:continents/:countries' 之类的内容。在它的开头添加一些硬编码字符串使其不再如此包罗万象。

    【讨论】:

    • 是的,这是我现在的最后手段:)。谢谢您的意见。为什么让它参数化 ('profile/:tabName') 使它拦截了预期的路由(配置文件,尽管 URL 混乱)而通配符没有?
    • B/c 如果你告诉路由器寻找参数,那么它会寻找profile/.+
    • 最终结果是:foo/:bar 样式的路由在您的根路由器中不适合您。
    • 我明白了,所以我唯一的两个选择是在它前面添加静态前缀 'earth' 或使 :countries 部分成为 :continents 的子路由..
    • 你必须给路由器一些匹配的东西。路由器是“贪婪的”,如果可以的话,它会匹配东西,它不会在匹配之前询问子路由器是否可以匹配路由,因为这会导致严重的性能问题。你已经给路由器一个路由匹配几乎任何有斜线的东西,所以它会吸收所有它可以吸收的东西。
    猜你喜欢
    • 2015-05-28
    • 2018-08-19
    • 1970-01-01
    • 2018-10-04
    • 2012-07-13
    • 1970-01-01
    • 2016-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多