Привет, я новичок в угловой 2+,
Я пытаюсь обмениваться данными между двумя компонентами, но второй компонент не получает данные из службы, он получает пустой объект.
Сервис - использование rxjs BehaviorSubject для сохранения объекта
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { Observable } from 'rxjs/Observable';
@Injectable()
export class PostsService {
response: any = {};
private messageResponse = new BehaviorSubject(this.response);
currentResponse = this.messageResponse.asObservable();
constructor(private http: Http) { }
// Get all posts from the API
getAllPosts() {
return this.http.get('/api/posts')
.map(res => {
this.messageResponse.next(res.json());
return res.json();
}).catch(err => {
console.log('caught exception' + err.status);
return Observable.throw(err);
});
}
}
Компонент 1 - Посты. Этот компонент делает первый вызов, чтобы получить данные, и без проблем извлекается и обновляет messageResponse.
export class PostsComponent implements OnInit {
// instantiate posts to an empty array
posts: any = [];
constructor(private postsService: PostsService) { }
ngOnInit() {
// Retrieve posts from the API
this.postsService.getAllPosts().subscribe(posts => {
this.posts = posts;
});
}
}
Компонент 2 - Сообщения 2 - Этот компонент получает currentResponse, однако в журнале отображается пустой массив.
export class Posts2Component implements OnInit {
posts: any = [];
constructor(private postsService: PostsService) { }
ngOnInit() {
this.postsService.currentResponse.subscribe(posts => {
this.posts = posts;
console.log(this.posts);
});
}
}
Всякий раз, когда я просматриваю компонент Posts2, я не вижу никаких данных currentResponse. Я не уверен, что я сделал не так здесь?
Спасибо