如何在角组件中声明对象
我会在Angular组件中对对象声明有问题,
import { Component } from '@angular/core';
@Component({
selector: 'app-clases',
templateUrl: './clases.component.html'
})
export class ClasesComponent
{
alerta:string="alert-danger";
constructor() { }
propiedades:Object = {
danger: false
}
}
我是使用Angular的新手,当我尝试编译此代码时,
我会收到一个错误***汇编的问题:
Error: src/app/components/clases/clases.component.html:9:76 - error TS2339: Property 'danger' does not exist on type 'Object'.
9 <h3 [ngClass]=" {'text-danger':propiedades.danger,'text-info':!propiedades.danger }">
~~~~~~
src/app/components/clases/clases.component.ts:5:16
5 templateUrl: './clases.component.html'
~~~~~~~~~~~~~~~~~~~~~~~~~
Error occurs in the template of component ClasesComponent.
似乎班级中的危险性尚不存在,但是它在那里,所以给予该错误
有帮助吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您只需要添加变量的类型即可成为这样。
I think you just need to add the type of variable propiedades to become like this.
Typescript具有足够的信息来单独查找对象类型,因此您可以做到:
如果将其明确键入为
object
,并评估此模板行:Angular/tyspercript编译器将期望类型确实没有
危险
属性。Typescript has more than sufficient information to figure out the object type by itself, so you can just do:
If you explicitly type it as
Object
, and evaluate this template line:The Angular/TypeScript compiler will expect a value of type
Object
, which indeed has nodanger
property.有两种使用接口在打字稿中声明对象的方法。
第一 - 当我们知道确切的键时,我们必须使用一个接口,例如:
第二 - 当我们不知道确切的键时,我们必须使用一个接口:
There are 2 ways to declare Objects in TypeScript using the interface.
1st - When we know the exact keys then we have to use an interface like:
2nd - When we don't know exact keys then we have to use an interface like: