简体   繁体   English

如何仅在当前路线上挂起“活动”班级?

[英]How can I hang the “active” class only on the current route?

I have such routes: 我有这样的路线:

this.route('posts', function() {
  this.route('index', { path: '/' })
  this.route('show', { path: '/:post_path' }, function() {
    this.route('people', { path: '/people' })

    this.route('files', function() {
      this.route('images', { path: '/images' })
      this.route('videos', { path: '/videos' })
    })
  })
}),

I have a problem with the class "active". 我对“活动”类有疑问。

Take, for example, this link: 以以下链接为例:

http://localhost:4200/posts/1-best-post/files/images

In this case, the class "active" will hang on two links - on posts.show.files.images and on posts.show . 在这种情况下,“活动”类将挂在两个链接上posts.show.files.imagesposts.show

How to make the class "active" only for posts.show.files.images ? 如何使类仅对posts.show.files.images

ADDITION 加成

Apparently the problem is not only with the class "active". 显然,问题不仅在于“活动”类。 But with the templates. 但是有了模板。 The children use the "show" template. 孩子们使用“显示”模板。

Could you please explain how to properly describe such nested routes? 您能否解释一下如何正确描述此类嵌套路线?

The issue you're encountering is because ember has a hidden index route for each nested function in the router. 您遇到的问题是因为ember对于路由器中的每个嵌套函数都有一个隐藏的索引路由

Your route file actually expands out to look like this. 您的路线文件实际上会扩展为如下所示。

this.route('posts', function() {
  this.route('index', { path: '/' })
  this.route('show', { path: '/:post_path' }, function() {
    this.route('index', { path: '/' }) #New
    this.route('people', { path: '/people' })

    this.route('files', function() {
      this.route('index', { path: '/' }) #New
      this.route('images', { path: '/images' })
      this.route('videos', { path: '/videos' })
    })
  })
}),

When you define the link-to path, I'm going to assume that you specify it exactly as it looked, rather than using the hidden index. 当您定义链接路径时,我将假定您完全按其外观指定它,而不是使用隐藏索引。

# Don't do this
{{#link-to 'posts.show' "1234"}}Posts Show{{/link-to}}
{{#link-to 'posts.show.files.images' "1234"}}Images Show{{/link-to}}
# Do this
{{#link-to 'posts.show.index' "1234"}}Posts Show{{/link-to}}
{{#link-to 'posts.show.files.images' "1234"}}Images Show{{/link-to}}

This ember-twiddle shows an example of the issue you're having. 这项余烬式操作显示了您遇到的问题的示例。 The top section shows the issue you're experiencing. 顶部显示您遇到的问题。 The bottom section shows the corrected links. 底部显示了更正的链接。

Similarly, the show.hbs route you're using is going to be considered a wrapper for ALL routes under show. 同样,您正在使用的show.hbs路由将被视为show下所有路由的包装器。
If you don't want the content of show when you're looking at files or images, move the logic and display into posts/show/index.js and posts/show/index.hbs 如果您不希望在查看文件或图像时显示内容,请移动逻辑并显示在posts / show / index.js和posts / show / index.hbs中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM