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

查看:32
本文介绍了无法在 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 异步获取并在页面呈现后初始化,因此首先它为空.但是我认为把?在这个变量之后可以避免这个问题.

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天全站免登陆