Working on setting my language in the BehaviorSubject with a default value using a LanguageService. The service structure is as follows
import {Injectable} from '@angular/core';
import * as globals from '../../../environments/globals';
import {BehaviorSubject} from 'rxjs';
@Injectable()
export class LanguageService {
languages = globals.languages;
language = new BehaviorSubject<string>(this.languages.polish.code);
setLanguage(code: string) {
console.log(code)
this.language.next(code);
}
getLanguage() {
return this.language.asObservable();
}
}
This is provided by the SharedModule and imported into every single component I have. While using setLanguage, I can see the correct code being set. However, when I call getLanguage, it always returns me the default value. This has not been an issue with BehaviorSubject before, so I am unsure of the cause.
I have implemented
this.languageSubscription = this.languageService.getLanguage().subscribe((language: string) => {
console.log(language);
})
in one of my components to test, but unfortunately, it is not working. Any suggestions?
I suspect the problem is related to routing. When I switched to AsyncSubject or normal Subject, on changing screens, getLanguage() does not return anything as if it's never called.