gpt4 book ai didi

Angular:如何确定带参数的事件路线?

转载 作者:太空狗 更新时间:2023-10-29 16:57:51 25 4
gpt4 key购买 nike

我读过 this question about how to determine the active route ,但我仍然不清楚如何使用参数确定事件路线?

现在我是这样做的:

<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;
}

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

最佳答案

有了新的、即将到来的 Angular 2 路由器(在我写这篇文章时位于 version 3.0-alpha.7),它非常简单。

您需要做的就是在链接中使用 [routerLinkActive] 指令。

<a [routerLinkActive]="['active']" [routerLink]="['/contact',  contact.id ]">
{{contact.name}}
</a>

当 Angular 2 正式发布时,您将使用它,不再是候选版本。我现在正在使用路由器的 alpha,没有任何问题。

Here's Plunk demonstrating it .您可以看到链接在您单击时变为红色。这是将 active 类分配给它们的指令。当然,您可以使用您选择的任何类名。

关于Angular:如何确定带参数的事件路线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34571517/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com