angular的@ViewChild和@ViewChildren获取不到元素

发布于 2022-09-07 22:18:08 字数 1175 浏览 27 评论 0

import { Component, AfterViewInit, ViewChild, ElementRef, Renderer2, ViewChildren, QueryList } from '@angular/core';

@Component({
  selector: 'app-locations',
  templateUrl: './locations.component.html',
  styleUrls: ['./locations.component.scss']
})
export class LocationsComponent implements AfterViewInit {

  constructor(private elementRef: ElementRef, private renderer: Renderer2) { }
  @ViewChild('test', { read: ElementRef })
  public itemDiv: ElementRef;
  @ViewChildren('item')
  public itemDivs: QueryList<ElementRef>;
  public ngAfterViewInit() {
    setTimeout(() => {
      console.log('sssss', this.itemDiv);
      console.log('sssssb', this.itemDivs);
    }, 0);
  }

}
<div class="row no-gutters h-100" id="test">
  <div class="col-4 item">
    <app-locations-list></app-locations-list>
  </div>
  <div class="col-4 item">
  </div>
  <div class="col-4 item">
    One of three columns
  </div>
</div>

结果:
图片描述
请问我代码是哪里出了问题?新入门,请多多指教!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

GRAY°灰色天空 2022-09-14 22:18:09

html中应该是#test, 而不是设置id

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文