代码之家  ›  专栏  ›  技术社区  ›  reggaemahn

如何使用ember.js创建嵌套的动态路由,其中子路由替换父路由的模板?

  •  0
  • reggaemahn  · 技术社区  · 6 年前

    余烬3.15(辛烷值)

    /cars
    /cars/:car_id
    /cars/:car_id/:model_id
    

    前两个很好,我可以用 ember-cli

    ember g route cars
    ember g route cars/car
    

    model cars 模板,即更换 car {{outlet}} 模板在其中呈现得很好,但很明显 {{出口}} 当我导航到 /cars/5/2

    Router.map(function() {
      this.route('cars', function(){
        this.route('car', { path: ':car_id'}, function() {
          this.route('model', { path: ':model_id' });
        });
      });
    });
    

    我试过使用索引路由 ember g route cars/car/index 但问题是 params 在中不可用 index 汽车 路线。

    作为计划b,我创建了汽车和模型路线作为顶级路线( based on this answer ),但我想实现上述目标是因为以下几个原因

    • 我有许多嵌套的路由,创建它们作为第一级路由将变得很难维护
    • 余烬不应用 active Cars 作为一个链接,我希望它有 所有三页的样式。但这已经不起作用了,因为第二条路线将被称为 汽车 第三个差不多 .
    • 默认情况下使用 <LinkTo /> .

    如果我脑子里有这样的东西 模板

    <ul>
      {{#each @model.models as |model|}}
        <li><LinkTo @route="model" @model={{model}}> {{model.title}} </LinkTo></li>
      {{/each}}
    </ul>
    

    /cars/undefined/undefined . (虽然这是可以顺便解决的 @models={{array @model.id model.id}} ,这似乎是一个解决办法)

    0 回复  |  直到 6 年前
        1
  •  1
  •   Lux    6 年前

    解决办法确实是使用 index 路线。

    我也是 cars.car.index cars.car.model . 但是你应该使用 cars.car 加载公用程序的路径 car 模型。然后你可以在 路由方式 this.modelFor('cars.car')

    params.car_id 路线,使用它装载汽车,然后从 汽车索引 汽车.汽车.模型 modelFor .