我正在尝试使用BehaviorSubject将数据从一个组件共享到另一个组件,但是当我订阅 目标组件中的Observable时,它返回 空
这是我的服务级别
private apidata=new behaviorssubject<patient>(空); public apidata$=this.apidata.asobservable(); 获取单个患者(ID){ this.loadtoken(); let headers=new httpheaders().set('authorization',this.authtoken); 返回this.http.get('http://localhost:3000/api/patient/'+id,headers: 报头}; } SEDATA(数据){ this.apidata.next(数据); } < /代码> 这是我得到数据的地方 getsinglepatient(id:any){ this.patientservice.getsinglepatient(id).subscribe((data:any)=>。{ this.patients=data;//<--患者是一个数组 this.patientservice.setdata(数据); this.router.navigate(['/records']); console.log(数据); (}); < /代码> 这是我要显示/检索数据的目标类 patient:patient; 建造商(私人病人服务:病人服务){ this.patientservice.apidata$.subscribe(patient=>console.log(patient)); } < /代码> 这是我的控制台输出 我提到了这个示例 不知道我第一次这么做是怎么回事。谢谢。它返回无效的 这是我的服务类 private apiData = new BehaviorSubject<Patient>(null); public apiData$ = this.apiData.asObservable(); getSinglePatient(id) { this.loadToken(); let headers = new HttpHeaders().set('Authorization', this.authToken); return this.http.get('http://localhost:3000/api/patient/' + id, { headers: headers }); } setData(data) { this.apiData.next(data); } 这是我的小学班我从哪里得到数据 getSinglePatient(id: any) { this.patientService.getSinglePatient(id).subscribe((data: any) => { this.patients = data; //<-- Patients is an Array this.patientService.setData(data); this.router.navigate(['/records']); console.log(data); }); 这是我的目标类我想在哪里显示/检索数据 patient: Patient; constructor(private patientService: PatientService) { this.patientService.apiData$.subscribe(patient => console.log(patient)); } 这是我的控制台输出 我提到过这个example 不知道我第一次这么做是怎么回事。谢谢。
这是我得到数据的地方
getsinglepatient(id:any){ this.patientservice.getsinglepatient(id).subscribe((data:any)=>。{ this.patients=data;//<--患者是一个数组 this.patientservice.setdata(数据); this.router.navigate(['/records']); console.log(数据); (}); < /代码> 这是我要显示/检索数据的目标类 patient:patient; 建造商(私人病人服务:病人服务){ this.patientservice.apidata$.subscribe(patient=>console.log(patient)); } < /代码> 这是我的控制台输出 我提到了这个示例 不知道我第一次这么做是怎么回事。谢谢。它返回无效的 这是我的服务类 private apiData = new BehaviorSubject<Patient>(null); public apiData$ = this.apiData.asObservable(); getSinglePatient(id) { this.loadToken(); let headers = new HttpHeaders().set('Authorization', this.authToken); return this.http.get('http://localhost:3000/api/patient/' + id, { headers: headers }); } setData(data) { this.apiData.next(data); } 这是我的小学班我从哪里得到数据 getSinglePatient(id: any) { this.patientService.getSinglePatient(id).subscribe((data: any) => { this.patients = data; //<-- Patients is an Array this.patientService.setData(data); this.router.navigate(['/records']); console.log(data); }); 这是我的目标类我想在哪里显示/检索数据 patient: Patient; constructor(private patientService: PatientService) { this.patientService.apiData$.subscribe(patient => console.log(patient)); } 这是我的控制台输出 我提到过这个example 不知道我第一次这么做是怎么回事。谢谢。
这是我要显示/检索数据的目标类
patient:patient; 建造商(私人病人服务:病人服务){ this.patientservice.apidata$.subscribe(patient=>console.log(patient)); } < /代码> 这是我的控制台输出 我提到了这个示例 不知道我第一次这么做是怎么回事。谢谢。它返回无效的 这是我的服务类 private apiData = new BehaviorSubject<Patient>(null); public apiData$ = this.apiData.asObservable(); getSinglePatient(id) { this.loadToken(); let headers = new HttpHeaders().set('Authorization', this.authToken); return this.http.get('http://localhost:3000/api/patient/' + id, { headers: headers }); } setData(data) { this.apiData.next(data); } 这是我的小学班我从哪里得到数据 getSinglePatient(id: any) { this.patientService.getSinglePatient(id).subscribe((data: any) => { this.patients = data; //<-- Patients is an Array this.patientService.setData(data); this.router.navigate(['/records']); console.log(data); }); 这是我的目标类我想在哪里显示/检索数据 patient: Patient; constructor(private patientService: PatientService) { this.patientService.apiData$.subscribe(patient => console.log(patient)); } 这是我的控制台输出 我提到过这个example 不知道我第一次这么做是怎么回事。谢谢。
这是我的控制台输出
我提到了这个示例
不知道我第一次这么做是怎么回事。谢谢。它返回无效的
这是我的服务类
private apiData = new BehaviorSubject<Patient>(null); public apiData$ = this.apiData.asObservable(); getSinglePatient(id) { this.loadToken(); let headers = new HttpHeaders().set('Authorization', this.authToken); return this.http.get('http://localhost:3000/api/patient/' + id, { headers: headers }); } setData(data) { this.apiData.next(data); }
这是我的小学班我从哪里得到数据
getSinglePatient(id: any) { this.patientService.getSinglePatient(id).subscribe((data: any) => { this.patients = data; //<-- Patients is an Array this.patientService.setData(data); this.router.navigate(['/records']); console.log(data); });
这是我的目标类我想在哪里显示/检索数据
patient: Patient; constructor(private patientService: PatientService) { this.patientService.apiData$.subscribe(patient => console.log(patient)); }
我提到过这个example
不知道我第一次这么做是怎么回事。谢谢。
你需要设置 apiData$ 一旦你分配,
apiData$
setData(data) { this.apiData.next(data); this.apiData$ = this.apiData.asObservable(); }
编辑:
你不必做上述的事情,只需要在app.module级别配置服务就可以了。不是在每个类中都可以,因为它们每次都会创建一个实例,所以这些值都会消失。