Angular 路由切换后页面返回顶部

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_31851435/article/details/89211575

 由于Angular是单页面应用,所以在路由切换后页面的位置并不会像多页面那样重新刷新页面,滚动条置顶,为了解决这个问题,可以监听路由事件,在里面做滚动条置顶操作。


import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
 
declare var $: any;
 
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {

  constructor(public router: Router) {
 
  }
 
  ngOnInit() {
 
    this.router.events
      .subscribe((event) => {
        $(window).scrollTop(0);
      });
  }
 
}

猜你喜欢

转载自blog.csdn.net/qq_31851435/article/details/89211575