我的主题在Angular中无法正常工作

Rol*_*uza 0 rxjs angular

我像往常一样在服务文件中创建了一个主题

editLectures = new Subject<Lecture>();

getEditLectureListener() {
  return this.editLectures.asObservable();
}
Run Code Online (Sandbox Code Playgroud)

我正在从一个组件发送数据(我收到的是正确的数据console.log

onUpdate(lec: any) {
  this.attendanceService.emitLecture(lec);
}
Run Code Online (Sandbox Code Playgroud)

在另一个组件上,我正在听主题:

this.updateLecture = this.attendanceService.getEditLectureListener().subscribe(result => {
  // my code
  // on console.log(result) i am not getting anything and other listeners
  // are working perfectly, the only difference is that
  // its not emitting data from http response 
});
Run Code Online (Sandbox Code Playgroud)

在使用中,我正在发射数据:

emitLecture(lec: Lecture) {
  this.editLectures.next(lec);
  this.router.navigate(['edit-lecture']);
}
Run Code Online (Sandbox Code Playgroud)

Bil*_*eni 9

您需要使用BehaviorSubject而不是Subject,以便最后发出的值可用于新订阅者(实例化您的侦听器组件时)。

editLectures = new BehaviorSubject<Lecture>(null);
Run Code Online (Sandbox Code Playgroud)