小编Mat*_*ins的帖子

`?` 在某些对象属性之前是什么意思?为什么我的 ts 文件不接受它?

*ngIf当我有一个可能未定义的对象或所需对象之外的对象时,我总是在角度指令中使用这种行为显示

<div *ngIf="object?.foo">
  ...
</div>
Run Code Online (Sandbox Code Playgroud)

我知道这有效,但不知道为什么...

但是,当我尝试在我的TypeScript文件中的 if 上使用它时,如以下 angularTS1005, TS1109在我尝试使用此行为的每一行上,向我抛出两个不同的错误,相互交替。

if(object?.foo){} // error TS1109
if(object?.foo){} // error TS1005
if(object?.foo){} // error TS1109
if(object?.foo){} // error TS1005
Run Code Online (Sandbox Code Playgroud)

我在这个项目中使用 Angular 7.3.9

html javascript conditional-statements typescript angular

3
推荐指数
1
解决办法
153
查看次数