当使用Router.navigate进行路由导航时,Angular只会加载变化的组件,而不是整个页面。如果需要加载整个页面,可以使用以下方法:
// 跳转时传递数据 this.router.navigate(['/target-component'], { queryParams: { id: 1 } });
// 目标组件接收数据 import { ActivatedRoute } from '@angular/router';
constructor(private activatedRoute: ActivatedRoute) {}
ngOnInit() { this.activatedRoute.queryParamMap.subscribe(params => { const id = params.get('id'); // 对传递的数据进行处理 }); }
this.router.navigateByUrl('/', { skipLocationChange: true }).then(() => this.router.navigate(['/target-component']) );
注意:这种方法会重新加载整个页面,会导致当前页面上的所有状态、数据等都会被重置。因此,只有在必要的情况下才应该使用。