Cannot read property 'outlets' of null in Angular 4

非 Y 不嫁゛ 提交于 2019-12-04 16:02:10

问题


I have Angular 4.3.6 project where a template snippet produces this error.

Template block:

<a [routerLink]="['/article',article?.id]">{{article?.title}}</a>

Error stack trace:

ArticleSpComponent.html:26 ERROR TypeError: Cannot read property 'outlets' of null
    at createNewSegmentGroup (router.es5.js:2967)
    at updateSegmentGroup (router.es5.js:2896)
    at router.es5.js:2914
    at forEach (router.es5.js:593)
    at updateSegmentGroupChildren (

The error cause seems to be obvious. article variable is fetched async from Http and initialized after page is rendered so firstly it's null. However I thought that putting ? after this variable allows to avoid this issue.

Can you please advise?


回答1:


? in article?.id is working fine, but the RouterLink directive doesn't like getting a null passed.

You could work around using something like:

<a *ngIf="article" [routerLink]="['/article',article?.id]">{{article?.title}}</a>
<a *ngIf="!article">{{article?.title}}</a>


来源:https://stackoverflow.com/questions/45916039/cannot-read-property-outlets-of-null-in-angular-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!