所以我使用下面的帖子来弄清楚如何将片段添加到 url。
Angular2 Routing with Hashtag to page anchor
并且片段添加得很好,但是页面不会移动到生成的 anchor 。我尝试在目标 div
上同时使用 name
和 id
属性,但似乎都不起作用。
我使用的是 Angular 版本 2.0.0-rc.3 和路由器版本 3.0.0-alpha.6。
谢谢!
<a [routerLink]="[]" fragment="destination">Go TO DIV</a>
<div id='destination'>
<h2>Destination</h2>
</div>
两个元素之间有足够的空间来判断页面是否实际移动。
如前所述,url 正确地将 #destination
添加到自身。
最佳答案
解决方法可能是在您的组件中编写一个函数,将 location.hash 设置为您要导航到的 div 的 ID。
<a (click)="goTo('destination')">Go TO DIV</a>
<div id='destination'>
<h2>Destination</h2>
</div>
然后在你的组件中:
goTo(location: string): void {
window.location.hash = location;
}
关于html - Angular2 Hashtag路由到Anchor不会移动页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38877353/