【发布时间】:2019-10-03 09:43:30
【问题描述】:
我想使用带有子组件的 html 锚。我正在尝试使用@ViewChild,但值返回未定义。你能帮帮我吗?
我的头组件:
//My html
<button (click)="scrollToElement(target)"></button>
//My TS
@ViewChild('target', { read: ViewContainerRef }) target;
scrollToElement($element): void {
console.log($element);
$element.scrollIntoView({behavior: "smooth", block: "start", inline: "nearest"});
}
我的homeComponent(用来集中所有组件)
<app-header></app-header>
<app-anchor></app-anchor>
我的锚组件:
<div #target>My target</div>
【问题讨论】:
-
您是否尝试访问 AnchorComponent 中的 div 元素?你想要它在 HeaderComponent 中吗?
-
是的,我的主菜单在 headerComponent 中,我想到达 AnchorComponent 中的 div