简体   繁体   English

Angular:将解析器添加到无路由组件

[英]Angular: Add resolver to a route less component

I am working on an app where I have three different routes for a lazy loaded module.我正在开发一个应用程序,其中我为延迟加载的模块提供了三种不同的路由。 I am using router-outlet to display those routes and have a static component that remains the same through out these three routes.我正在使用 router-outlet 来显示这些路由,并有一个 static 组件在这三个路由中保持不变。 I want to access the data from the resolver in my static component but, I am getting undefined .我想从 static 组件中的解析器访问数据,但是,我得到了undefined

Here's the code:这是代码:

static.component.ts static.component.ts

ngOnInit(){
    const data = this.activatedRoute.snapshot.data['deliveryData'];
    console.log(data);    //undefined, since it's not a route
}

cart.resolver.ts购物车.resolver.ts

resolve(): Observable<any>{
    const payload = xyz;
    return this.http.postHttpRequest(payload);
}

The page looks like something like this:页面看起来像这样:

- <router-outlet></router-outlet>
- <static-component></static-component>

Therefore, I cannot use output.因此,我不能使用 output。 How can I resolve this?我该如何解决这个问题? Any help would be appreciated.任何帮助,将不胜感激。 Thanks!谢谢!

Try to go with services.尝试使用服务 go。

Create a shared service and set the delivery data using setDeliveryData(data) when you want and we can get the data in static component similar way like below.创建一个共享服务并在需要时使用setDeliveryData(data)设置交付数据,我们可以像下面这样在 static 组件中获取数据。

service.ts服务.ts

deliveryData: any = {};

publish void setDeliveryData(data : any) {
   this.deliveryData = data;
}

static.component.ts static.component.ts

ngOnInit(){
    const data = this.sharedService.getDeliveryData();
    console.log(data);    
}

I resolved this issue by declaring the static component as an Auxiliary Route .我通过将 static 组件声明为Auxiliary Route解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM