У меня есть сервис с геттером и сеттером:
// Service
import { Subject } from 'rxjs';
export class MyService {
public currentUser: any = new Subject();
... there is a function where I call setCurrent and assign value
set setCurrent(data: any) {
this.currentUser.next(data);
this._localStorage.set('user', JSON.stringify(data))
}
get setCurrent() {
return this._localStorage.get('user');
}
}
В моем компоненте я подписался на currentUser
:
// Component
public user: User;
constructor(
public authService: AuthService,
) {
this.authService.currentUser.subscribe(nextData => {
this.user = nextData;
console.log(nextData) // Expected value, it's here
console.log(this.user) // Undefined
})
}
Почему требуется возвращаемое и ожидаемое значение, но это не такприсвоить его компонентной переменной?