获取路由器出口外的路由数据 [英] Get route data outside the router outlet
问题描述
有没有办法在路由器出口的outisde组件中获取data
参数.
Is there a way to get the data
params in a component that is outisde of router outlet.
const appRoutes: Routes = [
{ path: '', component: SitesComponent },
{ path: 'pollutants/newpollutant', component: PollutantComponent, data: { new: true, crumbs: ["New pollutant"] } }
];
组件
export class BreadcrumbsComponent implements OnInit {
list: any[] = [] as any[];
constructor( private router: Router) {
}
ngOnInit() {
this.router.events.subscribe((val) => {
if (val instanceof NavigationEnd)
console.log(val);
});
}
}
如上捕获导航结束只给我网址.
Capturing navigation end as above only gives me url.
推荐答案
好吧,我不确定您是否能做到这一点,而且无论如何这听起来都不是一个好的模式.您可以通过具有共享单例服务和可观察对象的任何组件共享数据(状态).
Well, I am not sure if you can do this and in any case it doesn't sound like a good pattern. You can share data (state) through any component with a shared singleton service and observables.
而且不一定是亲子关系https://angular.io/guide/组件交互#parent-and-children-communicate-via-a-service
希望有所帮助.这基本上是您要问的,但不是来自路由器服务,而是自定义"共享服务.他们都使用可观察对象.
I hope that helped. It's basically what you are asking but not from the router service but a 'custom' shared service. Both of them use observables.
这篇关于获取路由器出口外的路由数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!