I'm trying to make some components friendship. In AppComponent I get user pay balance in ngOnInit and subscribe for balance update. AppComponent is the root component. When user make a purchase in PaymentComponent, I want to set user pay balance in AppComponent. For these purposes I use BehaviorSubject in AuthService (singleton, import only in AppModule).
userInfo.getValue() in PaymentComponent is work, but in AppComponent subscribe() I don't receive any updates. What's the problem?
Thanks.
AppComponent
@Component({
...
providers: []
})
export class AppComponent implements OnInit {
public balance: number;
constructor(private authService: AuthService) { }
ngOnInit() {
this.authService.getUserProfile();
this.authService.userProfile.subscribe(
(data) => { this.balance = data; },
(error) => {console.log("error"); }
);
}
}
AuthService
@Injectable()
export class AuthService {
public userInfo = new BehaviorSubject<Users>(null);
public userProfile = this.userInfo.asObservable();
getUserProfile() {
this.http.get<Users>(Globals.base_url + 'profile').subscribe(
data => {
this.userInfo.next(data);
},
error => { console.log(error); });
}
}
PaymentComponent
@Component({
...
})
export class PaymentComponent implements OnInit {
constructor(public authService:AuthService) { }
ngOnInit() {
this.http.post(Globals.base_url + 'balance_write_off', body).subscribe(
data => {
this.authService.userInfo.next(data);
//this.authService.userInfo.getValue();
},
error => console.log("error", error)
);
}
}
UPDATE. Stackblitz: https://stackblitz.com/edit/angular-ivy-enelht?file=src/app/payment.component.ts
this.http.post(Globals.base_url + 'balance_write_off', body)return anything ? Did you set a console log in your subscribe to see if it emits something ? - Quentin Fonck@Injectable({providedIn: 'root'})` and remove it from your app.module providers to see if it changes anything ? - Quentin Fonck