如何以角度向下滚动 ngOnInit?

MoH*_*dEy 4 angular

在某些情况下,我想在 Angular 中加载页面时向下滚动到某个部分。

请注意:我想向下滚动而不单击任何内容(即ngOnInit)。

我尝试了这个:在我的component.html文件中

<div #sectionSubscribe>
    HTML...
</div>
Run Code Online (Sandbox Code Playgroud)

在我的 component.ts 文件中

ngOnInit(){
    this.scrollToSubscribe(sectionSubscribe);
}
scrollToSubscribe($element){
    $element.scrollIntoView({
        behavior: 'smooth',
        block: 'start',
        inline: 'nearest'
    });
}
Run Code Online (Sandbox Code Playgroud)

但它不允许我这样做。请帮忙

Adr*_*rma 6

尝试这样:

@ViewChild("sectionSubscribe", { static: true }) sectionSubscribeDiv: ElementRef;

scrollToSubscribe(){
    this.sectionSubscribeDiv.nativeElement.scrollIntoView({ behavior: "smooth", block: "start" });
}
Run Code Online (Sandbox Code Playgroud)

工作演示