I have one service, one component and one resolver as following. There are some codes in Service Constructor, it should set value in Service, then when I link to certain path, the Resolver of this route would call certain Service Method, and the Method should be able to read the value which already set by Constructor.
Service:
@Injectable({
providedIn: 'root'
})
export class BuildjsonService {
public currentUser: User;
constructor() {
console.log('BuildjsonService constructor');
this.currentUser = JSON.parse(sessionStorage.getItem('currentUser'));
}
genReqContent(fmCode: string, caseKey: string, transKey: string, content: string){
let reqContent : ReqContent = new ReqContent();
console.log('genReqContent=' + JSON.stringify(this.currentUser));
reqContent.bankerId = this.currentUser.loginID;
return reqContent;
}
Click on certain route, rosolver fired:
@Injectable()
export class GetSubDeptResolver implements Resolve<any> {
constructor(private httpClient: HttpClient,
private buildJsonSrv: BuildjsonService){}
resolve(): Observable<any> {
<!-- calling service-->
let reqCont = this.buildJsonSrv.genReqContent('', '', '', content);
let reqJsonObj: ReqJsonObj = this.buildJsonSrv.genJsonReq(reqCont, new FlowContent(), new TxnInfoContent());
return this.httpClient.post<any>('http://localhost:2387/utility/GetSubDept', reqJsonObj)
}
}
However, when Resolver calling genReqContent() in Service, 'this.currentUser' is null.
I can see that this Service is loaded and constructor is called when App starts, so the currentUser should already has value, however it's gone when being used later.
Am I doing something wrong? Thank you.