当咱们在第一个路由滑动到底部当咱们点击导航跳转到另外一个路由时页面没有回到顶部而是保持上一个路由的滚动位置,基本的解决办法有两种。angular2
import { Component, OnInit } from '@angular/core'; import { Router, NavigationEnd } from '@angular/router'; @Component({ selector: 'my-app', template: '<ng-content></ng-content>', }) export class MyAppComponent implements OnInit { constructor(private router: Router) { } ngOnInit() { this.router.events.subscribe((evt) => { if (!(evt instanceof NavigationEnd)) { return; } window.scrollTo(0, 0) }); } }
scrollPositionRestoration
参数(angular6以后)。RouterModule.forRoot(routes, {scrollPositionRestoration: 'enabled'})