在 Angular 15 中使用接口迫使我们删除操作链并在模板中抛出错误,错误:对象可能是“未定义”

问题描述 投票:0回答:1
angular typescript eslint
1个回答
0
投票

根据我的理解,只需要对链进行一次安全检查

?.
,这样你就可以简化代码。

<div *ngFor="let table of invData.tables;trackBy: utilServ.trackByFnIndex"> <!-- removed ?. because tables will not be undefined as per the interface! -->
    <table>                                                     
        <thead *ngIf="invData?.columns?.[table]?.length > 0"></thead> <!-- will safe check the sequence and prevent any undefined errors -->
    </table>
</div>
© www.soinside.com 2019 - 2024. All rights reserved.