使用 ViewChild 装饰器获取一个元素
<p #paragraph>hello works!</p>export class HelloComponent implements AfterViewInit { @ViewChild("paragraph") paragraph: ElementRef<HTMLParagraphElement> | undefined ngAfterViewInit(): void { console.log(this.paragraph?.nativeElement) }}
使用 ViewChildren 获取一组元素
<ul> <li #items>a</li> <li #items>b</li> <li #items>c</li></ul>export class HelloComponent implements AfterViewInit { @ViewChildren("items") items: QueryList<HTMLLIElement> | undefined ngAfterViewInit(): void { console.log(this.items?.toArray()) }}