Angular:如何使用参数确定活动路线? [英] Angular: How to determine active route with parameters?

查看:22
本文介绍了Angular:如何使用参数确定活动路线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了这个关于如何确定活动路线,但我仍然不清楚如何使用参数确定活动路线?

I've read this question about how to determine the active route, but still it's not clear to me how to determine an active route with paramaters?

现在我是这样做的:

<a [routerLink]="['/Profile/Feed', {username: username}]"
   [ngClass]="{active: getLinkStyle('/profile/john_doe/feed')}">
   Feed for {{username}}
</a>

在我的组件内部:

getLinkStyle(path:string):boolean {
  console.log(this._location.path()); // logs: '/profile/john_doe/feed'
  return this._location.path() === path;
}

这会起作用,因为我将用户名作为字符串传递.有没有办法通过传递正确的参数来做到这一点?

And this will work because I'm passing the username as a string. Is there any way to do this with passing the correct parameter??

推荐答案

只需检查自动定义的 router-link-active 类到 a 元素.

Just check the auto-defined router-link-active class to a element.

这篇关于Angular:如何使用参数确定活动路线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆