angular - 如何忽略 typescript 或 idm 错误?

标签 angular typescript visual-studio-code error-handling

我有一个 Angular 项目,在项目中,我有一个服务,它返回一个对象,在应用程序组件中,我正在使用该对象。

theobject.profileImg

一切正常,但 Idm 显示此错误。 “'object' 类型上不存在属性 'profileImg'。”

我正在使用 vsCode

enter image description here

最佳答案

在模板中使用如下:

theobject?.profileImg

或者
theobject['profileImg']

但通常更好地将模型添加到您的对象中,例如:
theobject: IObject;


interface IObject {
 profileImg: string;
 id: number;
 //and more
}

关于angular - 如何忽略 typescript 或 idm 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59749858/

相关文章:

visual-studio-code - 更漂亮/VSCode Eslint 奇怪的格式/语法破坏错误

angular - 直接从 html DOM 路由到 Component

javascript - 为什么我需要输入该命令两次才能在 Typescript、Ionic 2 中运行?

javascript - 用值初始化 Guard

c++ - Visual Studio Code - 未找到 SFML 库

visual-studio-code - 为什么我的一些扩展在 VS Code 1.57 中不起作用?

javascript - 选择时的 Angular 自动完成获取所选值的所有对象

checkbox - Angular 2 Checkbox preventDefault

angular - ionic 按钮单击不启动 ionic 4中的功能

Angular - 正确使用 RXJS 扩展运算符进行递归 http 调用