0

Earlier when I didnt had separate module for profile I didn't have to place profileDetails resolver inside only the dashboard component parent route and in all my componets i am accessing profile details as follows

 activeRoute.parent.data.subscribe((data)=> {
  this.userDetail = data.profileDetails.data;
});

Now I need to put profile detail resolve in all the sub modules of dashboard module and every time I go inside the compolent the profile detail api in the network tab gets called 2 times instead of one. I think one is called from the dashboard routing module and other from the individual module like profile module in this case.

App routing module

    path: 'dashboard',
    loadChildren: './dashboard/dashboard.module#DashboardModule'

Dashboard Routing Module

const routes: Routes = [
  {
    path: '', component: DashboardComponent,
    canActivate: [AuthGuard],
    runGuardsAndResolvers: "always",
    resolve: {
      profileDetails: ProfileDetailsResolverService,
    },  
    children: [
     {
       path: '', 
       loadChildren: './dashboard-home/dashboard-home.module#DashboardHomeModule'
     },
     {
         path: 'my-profile',
         loadChildren: './my-profile/my-profile.module#MyProfileModule'
      }

Profile Routing Module (I have to place profile details resolve here and all the other components, is there a way to not put it here and access the resolve which is already placed in the dashboard routing module)

    const routes: Routes = [
  { 
    path: '', resolve: {
      profileDetails: ProfileDetailsResolverService,
    },
    children: [
      {
        path: '', component: MyProfileComponent,
        data: { title: 'Ddb | Profile' }
      }
    ]
  }
];

1 Answer 1

1

You can use the resolver only in the parent component and create another service with the profile details that will be injected on every other component you want like this :

import { Injectable } from '@angular/core';
import { Observable, Subject } from 'rxjs';

 @Injectable({ providedIn: 'root' })

 export class ProfileDetailsService {

 private subject = new Subject<Details>();

 sendDetails(currentDetails: Details) {
    this.subject.next(currentDetails);
 }


 getDetails(): Observable<Details> {
    return this.subject.asObservable();
 }
}

On the ProfileDetailsResolverService :

this.profileDetailsService.sendDetails(details);

And finally subscribe to it wherever you want to have these data :

this.profileDetailsService.getDetails().subscribe(currentDetails => {
this.details= currentDetails;
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I am a bit confused as to where to call this api in that code to get the profile details getUserProfile() { return this.http.get(this.userProfileApi); in other where to make the http request to get the profile and make sure its not called every time }`
You should make the api call inside your resolver in order to be sure that you will have the data you need before rendering your components

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.