角度:组件上的scrollIntoView

时间:2019-06-21 11:35:13

标签: angular typescript

我有这个使用@ViewChild注释的子组件引用:

@ViewChild(UserSubComponent)
private userManagementComponent: UserSubComponent;

我想使用此引用来执行scrollIntoView

有可能吗?

1 个答案:

答案 0 :(得分:2)

更改选择器,使其返回ElementRef。

@ViewChild(UserSubComponent, {read: ElementRef})
private userManagementComponent: ElementRef;

然后在需要时使用本机元素滚动到视图中

ngOnInit(){
  this.userManagementComponent.nativeElement.scrollIntoView({ behavior: 'smooth', block: 'start' });
}