在路由器出口外获取路由数据 [英] Get route data outside the router outlet

查看:80
本文介绍了在路由器出口外获取路由数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有办法在路由器出口超出的组件中获取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);
    });
}

}

如上所述捕获导航端只会给我URL.

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/组件互动#父母和孩子通过服务进行沟通

And it doesn't have to be parent-child relationship https://angular.io/guide/component-interaction#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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆