link-to在index.hbs中工作,但在application.hbs中不起作用

时间:2017-02-15 21:18:03

标签: ember.js

当我从{{#link-to "cars/category" "mini"}}开始index.hbs时,一切正常。我转换到cars/category - > templates/cars/category.hbs

但是,当我从application.hbs(导航)执行相同操作时,我会转换到空白页面,然后自动转换到父路线cars.index - > templates/cars/index.hbs

这可能有一些逻辑。如何从application.hbs中的链接点击过渡到此路线?

(硬链接<a href="/cars/mini"工作正常,但我将丢失应用程序的状态。)

routes/cars/category.js型号:

model(params) {
    return this.store.findRecord('cars/category', params.category_id, {backgroundReload: false});
}

route.js

this.route('cars', () => {
    this.route('cars/category', { path: '/cars/:category_id' });
});

3 个答案:

答案 0 :(得分:1)

以下route.js

this.route('cars', function(){
    this.route('category', { path: '/:category_id' });
});

您可以执行{{#link-to "cars.category" "mini"}},这将转换为/cars/mini网址。

您不需要cars/category。因为它已经嵌套在cars路线内。 创建了Sample Twiddle

为了更好地理解路由,请参阅AlexSpeller ember-diagonal

答案 1 :(得分:1)

对于Ember 2.11

您是否尝试将路径路径更改为点符号cars.category

示例嵌套路由

Router.map(function() {
  this.route('photos', function(){
    this.route('photo', { path: '/:photo_id' }, function(){
      this.route('comments');
      this.route('comment', { path: '/comments/:comment_id' });
    });
  });
});

包含多个广告的正确link-to辅助链接,

  {{#link-to 'photo.comment' 5 primaryComment}}
    Main Comment for the Next Photo
  {{/link-to}}

您可以在

了解更多信息

https://guides.emberjs.com/v2.5.0/templates/links/#toc_example-for-multiple-segments

答案 2 :(得分:1)

有人建议,由于我转换到所需的页面然后立即转换到其父级,因此必须有其他一些转换排队。

进一步检查时,link-to确实位于列表项的下拉菜单中,该列表项本身也是link-to

解决方案是将bubbles=false添加到内部link-to

这里的其他答案怀疑使用的路线。但是,他们很好并且设置这样是有原因的。例如。具有名为category的子路由的多个路由不能都在根目录中。但是,我没有透露将人们置于错误轨道的确切代码,这是我的错,因为他们可能会立即注意到实际问题。

下次我的代码会更详细。我道歉,谢谢你和我一起思考。