角度6 |路由器链接

时间:2018-08-15 14:41:54

标签: angular typescript router-outlet

是否有任何方法可以转到由id定义的另一页部分,而无需重新加载

2 个答案:

答案 0 :(得分:1)

Angular v6.1中添加了许多新的滚动功能。

这是一篇博客文章的引文:

  

路由器滚动条提供与滚动到   角路由器。使用Router Scroller,您可以执行以下操作。

     

浏览器返回时在过渡之前还原到滚动位置   像#foo这样的分段URL,并使用   对应的ID

我假设第二段内容(锚定滚动)是您要问的?

如果是这样,您可以在此处找到更多信息:

https://medium.com/lacolaco-blog/introduce-router-scroller-in-angular-v6-1-ef34278461e9

还有

https://blog.ninja-squad.com/2018/07/26/what-is-new-angular-6.1/

答案 1 :(得分:1)

您可以使用scrollIntoView方法

贷方到:ibenjelloun

示例:https://stackblitz.com/edit/angular-scroll-spy-routing

@HostListener('scroll', ['$event'])
onScroll(event: any) {
    let currentSection: string;
    const children = this._el.nativeElement.children;
    const scrollTop = event.target.scrollTop;
    const parentOffset = event.target.offsetTop;
    for (let i = 0; i < children.length; i++) {
        const element = children[i];
        if (this.spiedTags.some(spiedTag => spiedTag === element.tagName)) {
            if ((element.offsetTop - parentOffset) <= scrollTop) {
                currentSection = element.id;
            }
        }
    }
    if (currentSection !== this.currentSection) {
        this.currentSection = currentSection;
        this.sectionChange.emit(this.currentSection);
    }
}