Angular 2,为路由器链接添加活动类[最佳实践]

问题描述 投票:2回答:1

我正在使用InputrouterLinkActive将活动类添加到某个特定的URL。但我希望如果特定链接检测到一个字块,那么该特定链接始终处于活动状态。让我解释一下:如果我定义的路线是/rent/page/:id,并进入我的模板,我这样做:

 <a [routerLinkActive]="['active']" [routerLink]="['/rent/page/1']" title="">rent</a>

如果地址栏中的url与定义的根匹配,则它会起作用,并且它会添加qazxsw poi类。包含租借链接的主导航将处于活动状态。

但我想要更多,如果我的路由有这个定义active,我点击网址,它不会将活动链接添加到我的主导航。

我找到了以下解决方案:

/rent/:property/:name/:id

每当我将 // ... this._router.events.subscribe( (urlObj) => { if(urlObj.url.match(/^(\/rent).*$/)) { this.active["rent"] = true; } } ) // ... 与当前网址匹配时,我将属性rent设置为对象rent,并将其添加到我的租借导航中。之前提到的模板,我将其改为:

active

它有效,但我不确定,这是不是一个好的做法。

我的问题是:如果我想保持特定链接活动,即使用户更改页面,如何<a [class.active]="active['rent']" [routerLink]="['/rent/page/1']" title="">rent</a> ?有没有其他方法可以使用routerLinkActive来做到这一点?

angular routerlinkactive
1个回答
0
投票

这可以通过router-link这样完成:

[routerLinkActive]

/ bar“>去酒吧

查看以下小提琴:<router-link :to="{name :'root'}">Root</router-link> <router-link :to="{name :'foo'}">Go to Foo</router-link> <router-link to="to="/bar">Go to Bar</router-link>

© www.soinside.com 2019 - 2024. All rights reserved.