【问题标题】:Angular route and object possibly undefined角度路线和对象可能未定义
【发布时间】:2021-03-18 11:50:39
【问题描述】:

我正在尝试在我的应用程序中导入此示例: https://stackblitz.com/edit/angular-breadcrumb-router

但在文件 breadcrumb.component.ts 中,Visual Studio 代码说

Object is possibly 'undefined'.ts(2532)

对于这一行:

const label = route.routeConfig ? route.routeConfig.data['breadcrumb'] : 'Home';

所以我不能使用它,我不明白为什么。

【问题讨论】:

  • 2 问题...您是否从示例中正确设置了路由,如果是,您是否尝试过控制台记录 route.routeConfig.data 中的内容?

标签: angular typescript routes breadcrumbs


【解决方案1】:

使用可选链接?运营商:

const label = route?.routeConfig?.data['breadcrumb'] ?? 'Home';

看看How can I solve the error 'TS2532: Object is possibly 'undefined'?

我猜你使用的是较新版本的 Typescript 或以某种方式激活了该规则。

【讨论】:

    猜你喜欢
    • 2021-01-24
    • 2021-01-06
    • 2020-12-01
    • 1970-01-01
    • 2021-06-06
    • 1970-01-01
    • 2018-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多