无法绑定到“routerLink",因为它不是“a"的已知属性 [英] Can't bind to 'routerLink' since it isn't a known property of 'a'
问题描述
我正在将我们的一个应用程序从 rc4 更新到 angular2.0.0,但在运行时遇到模板解析错误.这是我的视图模板:
<a [routerLink]="['/Tile', widget.WidgetRoute.Feature, widget.WidgetRoute.SubFeature]"><div class="tile-icon"><span [className]="widget.IconCssClass"></span><span *ngIf="widget.BadgeNumber > 0" class="badge">{{widget.BadgeNumber}}</span><h4>{{widget.Text}}</h4></a>
错误出在路由器链接上.这是错误:
无法绑定到routerLink",因为它不是a"的已知属性.("<div *ngFor="let widget of widgets" class="col-xs-3 quick-link"><a [ERROR ->][routerLink]="['/Tile', widget.WidgetRoute.Feature, widget.WidgetRoute.SubFeature]"><di"): LdrComponent@4:19无法绑定到ngIf",因为它不是span"的已知属性.(瓷砖图标"><span [className]="widget.IconCssClass"></span><span [ERROR ->]*ngIf="widget.BadgeNumber > 0" class="badge">{{widget.BadgeNumber}}</span>
"): LdrComponent@7:22
routerLink 对我来说似乎没有格式错误...我做错了什么?
问题是您忘记将 RouterModule
添加到您的 NgModule
组件中.在 RC 中,这被添加到 @Component({directives: [ROUTER_DIRECTIVES]})
,然而,现在被移动到 @NgModule({imports: [RouterModule]})
代码>.
当您定义路由时,您将导入的组件之一将是 RouterModule
,您将使用它来调用 forRoot
或 forChild
>.当您导入路由时,这将自动导入.
因此,您可以通过这种方式或通过直接导入到 @NgModule
的 imports
属性来获取 RouterLink.
I'm updating one of our apps from rc4 to angular2.0.0 and I'm getting a template parse error at run time. Here is my view template:
<div *ngFor="let widget of widgets" class="col-xs-3 quick-link">
<a [routerLink]="['/Tile', widget.WidgetRoute.Feature, widget.WidgetRoute.SubFeature]">
<div class="tile-icon">
<span [className]="widget.IconCssClass"></span>
<span *ngIf="widget.BadgeNumber > 0" class="badge">{{widget.BadgeNumber}}</span>
</div>
<h4>{{widget.Text}}</h4>
</a>
</div>
And the error is on the routerlink. Here's the error:
Can't bind to 'routerLink' since it isn't a known property of 'a'. ("
<div *ngFor="let widget of widgets" class="col-xs-3 quick-link">
<a [ERROR ->][routerLink]="['/Tile', widget.WidgetRoute.Feature, widget.WidgetRoute.SubFeature]">
<di"): LdrComponent@4:19
Can't bind to 'ngIf' since it isn't a known property of 'span'. ("tile-icon">
<span [className]="widget.IconCssClass"></span>
<span [ERROR ->]*ngIf="widget.BadgeNumber > 0" class="badge">{{widget.BadgeNumber}}</span>
</div>
"): LdrComponent@7:22
The routerLink doesn't seem malformed to me... What did I do wrong?
The problem is that you forgot to add RouterModule
to your NgModule
component. In the RC this was added to the @Component({directives: [ROUTER_DIRECTIVES]})
, however, this is now moved into @NgModule({ imports: [RouterModule]})
.
When you define your routes, one of the components that you will import will be the RouterModule
that you will use to call forRoot
or forChild
. When you import the route, this will be imported automatically.
So, you will get the RouterLink either this way, or via direct import into imports
property of @NgModule
.
这篇关于无法绑定到“routerLink",因为它不是“a"的已知属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!