【发布时间】:2019-11-04 12:15:41
【问题描述】:
我正在研究 angular 6,当我在没有任何条件的情况下添加路由器链接 (*ngIf) 时,路由器链接会抛出“Cannot read property 'outlets' of null”但是当我在特定部分中添加条件时它不会抛出任何错误链接。
例子:
<section>
<a [routerLink]="['/user',x?.username]"> {{x?.username}} </a>
</section>
上面的代码抛出一个错误。 (无法读取 null 的属性“出口”) 但是
<section *ngIf="x?.username != null">
<a [routerLink]="['/user',x?.username]"> {{x?.username}} </a>
</section>
这个不会抛出任何错误。
有人可以指导我吗?
提前致谢。
【问题讨论】:
-
当 [routerLink] 收到空值时会发生该错误。您可以放置 ngIf 以避免错误。