Home > database >  Angular: Add resolver to a route less component
Angular: Add resolver to a route less component

Time:12-04

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. I want to access the data from the resolver in my static component but, I am getting undefined.

Here's the code:

static.component.ts

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

cart.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. How can I resolve this? Any help would be appreciated. Thanks!

CodePudding user response:

Try to go with services.

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.

service.ts

deliveryData: any = {};

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

static.component.ts

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

CodePudding user response:

I resolved this issue by declaring the static component as an Auxiliary Route.

  • Related