[Angular] ViewChild 'read' option

It is not clear in the Docs about {read: xx} option for @ViewChild.

Based on the Source code, @ViewChild as view as Component, ElementRef, TemplateRef.

For example, we have:

<one></one>
  @ViewChild(OneComponent)
  one: OneComponent;
  ngAfterViewInit() {
    console.log('after', this.one);
  }

Now we read 'OneComponent' as a component, if we want to read it as ElementRef, we can do:

  @ViewChild(OneComponent, {read: ElementRef})
  one: ElementRef;

猜你喜欢

转载自www.cnblogs.com/Answer1215/p/10192285.html