在Angular中,我们可以使用类型化的空数组作为对象的属性。以下是一个示例:
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
template: `
{{ title }}
- {{ item }}
`,
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'Empty Array as Typed Object Property';
items: string[] = []; // 使用类型化的空数组作为属性
constructor() {
this.items.push('Item 1');
this.items.push('Item 2');
this.items.push('Item 3');
}
}
在上面的示例中,我们在AppComponent
中声明了一个名为items
的属性,类型为string[]
,并将其初始化为空数组[]
。然后,在构造函数中,我们使用this.items.push()
方法向数组中添加了一些元素。
在模板中,我们使用*ngFor
指令遍历items
数组,并将每个元素显示为一个列表项。
这样,我们就可以使用空数组作为类型化对象的属性,并在需要的时候向数组中添加元素。