我有一个用Angular 6编写的单页应用程序。要求将页面的每个选项卡都加载到顶部,因此我在主导航组件上实现了一个功能(整个应用程序都在使用该功能) ),就像这里一样:Angular 2 Scroll to top on Route Change,@ Fernando Echeverria的答案。
代码示例:
ngOnInit(): void {
this.location.subscribe((ev:PopStateEvent) => {
this.lastPoppedUrl = ev.url;
})
this.router.events.subscribe((ev) => {
if (ev instanceof NavigationStart) {
if (ev.url != this.lastPoppedUrl)
this.yScrollStack.push(window.scrollY);
} else if (ev instanceof NavigationEnd) {
if (ev.url == this.lastPoppedUrl) {
this.lastPoppedUrl = undefined;
window.scrollTo(0, this.yScrollStack.pop());
} else
window.scrollTo(0, 0);
}
});
问题是,如果我想在某种文本墙类型的页面上访问内部锚点,它会在那里导航并立即向后滚动,例如:
<li>
<a href="/text-wall#some-place-A">Navigate to A</a>
</li>
//list of other navigation references
...
<a id="some-place-1" class="anchor">A</a>
//Some text
...
//Another anchor with some text following
还有其他人遇到此问题吗?滚动删除会导致人们没有在其顶部加载标签。