我正在尝试在我的应用程序中导入此示例: https://stackblitz.com/edit/angular-breadcrumb-router
但在文件 breadcrumb.component.ts
中,Visual Studio Code 说
Object is possibly 'undefined'.ts(2532)
对于这一行:
const label = route.routeConfig ? route.routeConfig.data['breadcrumb'] : 'Home';
所以我不能使用它,我不明白为什么。
答案 0 :(得分:0)
使用可选链?。操作员:
const label = route?.routeConfig?.data['breadcrumb'] ?? 'Home';
看看How can I solve the error 'TS2532: Object is possibly 'undefined'?
我猜您使用的是较新版本的 Typescript 或以某种方式激活了该规则。