无法在Angular 4中读取null的属性“出口" [英] Cannot read property 'outlets' of null in Angular 4

查看:117
本文介绍了无法在Angular 4中读取null的属性“出口"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Angular 4.3.6项目,其中的模板代码段会产生此错误.

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

模板块:

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

错误堆栈跟踪:

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 (

错误原因似乎很明显. article变量是从Http异步获取的,并在呈现页面后初始化,因此首先它为null.但是我以为推杆呢?在此变量后面可以避免出现此问题.

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.

可以请教吗?

推荐答案

article?.id中的

?工作正常,但是RouterLink指令不喜欢通过null.

? 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>

这篇关于无法在Angular 4中读取null的属性“出口"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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