[英]How do I pass data from angular component to another component via router?
I'm tried to pass data from angular component to another component via a router.
我试图通过路由器将数据从角度组件传递到另一个组件。 Any elegant solution?
任何优雅的解决方
I tried this and it's working. 我试过这个并且它正在工作。
this.router.config.forEach(route => {
if (route.path === 'pages') {
route.children.forEach((child) => {
if (child.path === 'saveName') {
child.data = {
name: "Jack"
};
}
});
}
});
this.router.navigate(['/pages/saveName']);
And also send a file with data.
并且还发送包含数据的文件。
Just want to know whether there is a more elegant solution, like the one below.
只想知道是否有更优雅的解决方案,如下图所示。
this.router.navigate(['/pages/saveName'],{"name":"Jack"}); this.router.navigate([ '/页/ SAVENAME'],{ “名称”: “杰克”});
是的,请查看文档 :
this.router.navigate(['/heroes', { id: heroId, foo: 'foo' }]);
I think is better solution.Check following above Link. 我认为是更好的解决方案。请查看上面的链接。
https://angular.io/guide/router#fetch-data-before-navigating https://angular.io/guide/router#fetch-data-before-navigating
Angular 7.2 supports passing data via a state object during navigation Angular 7.2支持在导航期间通过状态对象传递数据
You can pass the state object in navigateByUrl method 您可以在navigateByUrl方法中传递状态对象
this.router.navigateByUrl('/path', { state: { hello: 'world' } });
or 要么
Pass Data using routerLink directive 使用routerLink指令传递数据
<a routerLink="/path" [state]="{ hello: 'world' }">Go</a>`
And Read the data using paramMap Observabele to filter only window.history.state 并使用paramMap Observabele读取数据,仅过滤window.history.state
constructor(public activatedRoute: ActivatedRoute) {}
ngOnInit() {
this.state$ = this.activatedRoute.paramMap.pipe(
map(() => window.history.state)
);
}
Check this link: https://netbasal.com/set-state-object-when-navigating-in-angular-7-2-b87c5b977bb 点击此链接: https : //netbasal.com/set-state-object-when-navigating-in-angular-7-2-b87c5b977bb
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.