解决angular单页面页面底部跳转到新页面滚动条不在顶部的问题

 以上jquery,下面js

this.router.events.subscribe((event) => {
	document.body.scrollTop=0;
});

 

另一种写法

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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.less','../assets/css/style.less'],
  encapsulation: ViewEncapsulation.None //Emulated当前  None全局
})
export class AppComponent {
  title = '数据平台';
  constructor(private  router: Router){
  	
  }
  ngOnInit(){
  	 this.router.events.subscribe((event:NavigationEnd) => {
  		if (event instanceof ActivationEnd){// 当导航成功结束时执行
	    	document.documentElement.scrollTop = 0;
	    	//document.body.scrollTop.scrollTop = 0;
  		}
		});
  }
}

  

  

 

posted @ 2019-04-18 11:32  一城柳絮吹成雪  阅读(1718)  评论(0编辑  收藏  举报