Get global data everywhere in app angular

45 views Asked by At

I have global variables that I need to access everywhere in my app. My project use subject as service state management. I don't want to use NGRX or redux pattern based state management.

I want to get data from Api and set variable in a service when the app loads, whatever the page. Then data will be get in components from the service.

Should I use a resolver on top parent route as in the code below? I want this api call to be executed only once.

const subRoutes: Route[] = [
  { path: 'home', component: HomeComponent },
  { path: 'products', component: productsComponent },
];

export const routes: Route[] = [
  {
    path: '',
    canActivate: [AuthGuard],
    children: subRoutes,
    resolve: { globalVariables: globalVariablesResolver },
  },
];

GlobalVariable resolver will save data in behaviorSubjects in a service. Is it the poper way to do it as I will get data from service and not from activatedRoute.data ?

0

There are 0 answers