Angular10 路由跳转到指定页面的指定位置

729 阅读1分钟

页面内的跳转,比较简单,要实现跨页面跳转,并跳转到指定地点

方法一:新增路由地址来实现

  • 要跳转的页面
<p id="readMore">hello</p>


ngOnInit() {
    if (window.location.hash === '#readMore') {
      window.location.assign('detail#readMore');
    }
}
  • 跳转的页面
this.router.navigateByUrl('/detail#readMore');

方法二:原路由地址不变的情况下,利用路由传递参数来实现

  • 要跳转的页面
<p id="readMore">hello</p>


this.myActivatedRoute.params.subscribe((data: any) => {
  if (data.id === 'readMore') {
    window.location.assign('detail#readMore');
  }
});
  • 跳转的页面
this.router.navigate(['/detail', { id: 'readMore'}]);