我有这个使用@ViewChild
注释的子组件引用:
@ViewChild(UserSubComponent)
private userManagementComponent: UserSubComponent;
我想使用此引用来执行scrollIntoView
。
有可能吗?
答案 0 :(得分:2)
更改选择器,使其返回ElementRef。
@ViewChild(UserSubComponent, {read: ElementRef})
private userManagementComponent: ElementRef;
然后在需要时使用本机元素滚动到视图中
ngOnInit(){
this.userManagementComponent.nativeElement.scrollIntoView({ behavior: 'smooth', block: 'start' });
}