Ultimately I want to use the getBoundingClientRect()
method to detect the bottom of my component and am using the @ViewChild
directive to do so. Normally in the past when doing this we need to access the nativeElement
of the element we target with the @ViewChild
however it doesn't seem to work on components.
As of right now I'm doing it this way which works
template
<section #intro>
<intro />
</section>
component
export class HomePageComponent implements AfterViewInit {
@ViewChild('intro', {static: true}) Intro?: ElementRef;
// irrelevant code
private detectIntroHeight():number{
return this.Intro?.nativeElement.getBoundingClientRect().bottom;
}
}
This code works. However if I do this
template
<intro #intro />
component
export class HomePageComponent implements AfterViewInit {
@ViewChild('intro', {static: true}) Intro?: IntroComponent;
// irrelevant code
private detectIntroHeight():number{
return this.Intro?.nativeElement.getBoundingClientRect().bottom;
}
}
I get an error saying nativeElement doesn't exist on IntroComponent. I've tried making it of type ElementRef<IntroComponent>
and got the same error. I also removed nativeElement just leaving the getBoundingClientRect()
method and got the same error saying it doesn't exist on IntroComponent.
Is there a way to make that work directly on my component without having to wrap it in an HTML element? I'd rather not have to wrap it just so I can detect where its located on the page.
Here's a basic stackblitz that demonstrates this issue.