0

我正在使用 Ember 3.18,我面临以下问题。考虑以下路线:

Router.map(function() {
  this.route('author');
  this.route('author' , {path:"/author/:author_id"});
});

现在,在我的 hbs 文件中,我正在尝试使用单个 LinkTo 转换到上述路由。如您所见,只有第二条路线需要模型属性。简单来说,我想以下 2 合并为一行。

<LinkTo @route="author" />
<LinkTo @route="author" @model="2" />

如您所见,我要求模型属性在某些情况下消失并在某些情况下可用。

请帮忙。

4

1 回答 1

2

我认为最简单的方法是稍微调整您的路由设置。我知道您想合并这些路线,但是 imo 很难/令人困惑,并且做一些更传统的事情会“更标准”,例如:

Router.map(function() {
  this.route('author', function() {
    this.route('view', {path:":author_id"});
  });
});

<LinkTo @route="author.index" />
<LinkTo @route="author.view" @model="2" />

author.index将匹配/author 并且author.view(with a @model) 将匹配/author/2.

请注意,索引是网络的隐式约定,在 router.js 文件中不需要

于 2022-02-24T20:04:58.480 回答