链接在子路线中设置为活动

Link is set to active in a sub route

本文关键字:设置 活动 链接      更新时间:2023-09-26

我注意到,当我在路由forums.archives中时,使用{{#link-to 'forums'}}Forums{{/link-to}}定义的链接被设置为类处于活动状态。

Router.map(function () {
    this.route('forums', function () {
        this.route('new');
        this.route('archives');
    });
});

导航:

{{#link-to 'forums'}}Forums{{/link-to}}
{{#link-to 'forums.archives'}}Forum archives{{/link-to}}

他们两个都上了现役。

如果链接上的活动类与url不100%匹配,有没有办法删除它?

感谢

所以您有一个嵌套的route,老实说,我不知道它是如何工作的。Ember团队的说法是,它的工作原理与嵌套资源相同,但有很多微妙之处(比如这个)并没有真正的意义。所以我会这么做(这可能不是你应该做的)。假设您的导航位于application模板中:

App.ApplicationController = Ember.ObjectController.extend({
    isForumsActive: function() {
        return (this.get('currentRouteName') === 'forums');
    }.property('currentRouteName')
});

然后,在您的模板中:

{{#link-to 'forums' active=isForumsActive}}Forums{{/link-to}}
{{#link-to 'forums.archives'}}Forum archives{{/link-to}}

你可能还想改变你的路线,这样嵌套就更干净了:

Router.map(function () {
    this.resource('forums', function () {
        // this.route('index'); -- implicitly defined
        this.route('new');
        this.route('archives');
    });
});

那么您的link-to可以改为指向forums.index