I am trying to load application level variables using API from the backend which will be used throughout the application. For the same I have using resolver but still that service is not getting loaded before any component is loaded, here is my code:
pub.global.service.ts
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { CustomUrlBaseService } from './auth/customurlbase';
import { Observable, Subject } from 'rxjs';
import { Resolve } from '@angular/router';
@Injectable()
export class PubService extends CustomUrlBaseService implements Resolve<any> {
appRoot = '/api/pub';
StoreData = '';
private loadPub = new Subject<any>();
loadPubObservable$ = this.loadPub.asObservable();
constructor(private http: HttpClient) {
super();
}
resolve() {
this.GetStoreData();
}
GetStoreData() {
this.GetData().subscribe(res => {
this.StoreData = res;
console.log(res);
})
}
GetData(): Observable<any> {
const url = (`${this.baseURL + this.appRoot}/GetStoreData`);
return this.http.get(url);
}
}
pub-footer.component.ts
import { PubService } from './../../services/pub.global.service';
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-pub-footer',
templateUrl: './pub-footer.component.html',
styleUrls: ['./pub-footer.component.scss']
})
export class PubFooterComponent implements OnInit {
copyrightinfo;
constructor(private pubService: PubService) { }
ngOnInit() {
console.log(this.pubService.StoreData);
const FooterData = this.pubService.StoreData;
this.copyrightinfo = FooterData['copyrightinfo'];
}
}
app.routing.ts
{
path: '',
component: PubLayoutComponent,
loadChildren: () => import('./views/pub/pub.module').then(m => m.PubModule),
data: { title: '' },
resolve: { items: PubService }
},
I also tried app_initializer, now my api is getting called twice but not any before component is loaded: In my module.ts
PubService, {
provide: APP_INITIALIZER, useFactory: initApp, deps: [PubService], multi: true
}
export function initApp(initService: PubService) {
return () => {
initService.GetStoreData();
}
}
My overall idea is to load Header and Footer information in just one api call and store in service variable and then get access of this json object from header and footer component. I even doubt the approach is correct.
Change Subject to BehaviorSubject
BehaviorSubject