In my angular app, when I login in to the app, I get an access token which I need to use it in my API calls in other components (not parent/child relationship). I tried using Shared service but it's not working
Data Service:
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs/Subject';
@Injectable()
export class DataService {
private userInfo = new Subject<any>();
currentuserInfo$ = this.userInfo.asObservable();
constructor() { }
updateUserInfo(message: any) {
this.userInfo.next(message)
}
}
In my login component I have
this.dataService.updateUserInfo(data.userToken);
Other Component
this.dataService.currentuserInfo$.subscribe((data: any) => {
console.log(data);
});
Here I am unable to get the tocken that was updated with the login component.
In the shared Service I tried both Subject and Behavior Subject but both didn't work.
I made sure that I just mentioned this DataService as a provider only in app.module.ts. I didn't mentioned it in login component or in any other component
the best way to add token in requests is to use interceptor
UPDATED:
if you need to create a shareable service - you can try to use the behavior subject. Please look at this example
UPDATED2:
if you want to use shareable data in spite of the page refreshing - you should use localstorage: