【发布时间】:2023-02-23 13:18:35
【问题描述】:
我在 angular8 中通过 url 传递一个值。无法从主要组件获取传递值。此外,id=2 将在 url 中可见,但 gt 不应在 url 中可见。这就是我使用 skipLocationChange 的原因。 skipLocationChange=true 无法正常工作。那么,如何解决这个问题?
header.component.ts:
nextComp() {
this.router.navigate(['/dashboard'], { queryParams: { id: '2' } });
this.router.navigate([], {
relativeTo: this.route,
queryParams: {
gt: 'hc',
},
queryParamsHandling: 'merge',
skipLocationChange: true,
});
}
main.component.ts:
ngAfterViewInit() {
this.route.queryParams.subscribe((param: Params) => {
console.log(param['id']);
console.log(param['gt']);
if(param['gt'] == 'dc'){
alert("You are coming from dashboard component")
}
if(param['gt'] == 'hc'){
alert("You are coming from header component")
}
});
}
仪表板.component.ts:
nextComp() {
this.router.navigate(['/dashboard'], { queryParams: { id: '2' } });
this.router.navigate([], {
relativeTo: this.route,
queryParams: {
gt: 'dc',
},
queryParamsHandling: 'merge',
skipLocationChange: true,
});
}
【问题讨论】:
标签: angular typescript angular8 angular9