【问题标题】:Angular show page top on view角度显示页面顶部视图
【发布时间】:2018-11-12 09:07:04
【问题描述】:

需要帮助!我正在使用 Angular 4。在哪里使用侧边栏作为单独的组件并在侧边栏的右侧显示相应的数据。 (即,如果用户单击侧边栏上的链接 A1,它应该在侧边栏右侧的顶部显示 A1 的内容)。但它不起作用我也尝试使用简单的 HTML href 属性移动到特定的 div。它工作得很好,但我遇到的问题是当用户点击 B 的链接时,页面再次开始加载页面,因为 B 是另一个组件。有角度的解决方案吗?

而且如果我滚动到 A 的容器的中间并且在单击链接 B 后它没有显示容器顶部它只是从我离开容器 A 的中间显示。任何解决方案?

【问题讨论】:

  • 请添加与手头问题相关的部分代码
  • 除非您在stackblitz.com 上提供工作演示代码,否则无法回答您的问题。没有代码,我们无法调试。而且我们不能编写整个代码;)。准备好演示后告诉我们。

标签: angular angular-routing angular-routerlink


【解决方案1】:

上一个问题的简单解决方案:

import {Component} from '@angular/core';
import {NavigationEnd, Router} from '@angular/router';

@Component({
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss'],
})
export class AppComponent {

  constructor(
    private readonly router: Router
  ) {
    this.router.events.subscribe((e) => {
      if (e instanceof NavigationEnd) {
        window.scrollTo(0, 0)
      }
    });
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-21
    • 1970-01-01
    • 1970-01-01
    • 2016-03-09
    • 2020-08-19
    • 2022-07-12
    • 2016-12-11
    相关资源
    最近更新 更多