Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 使用观察者作为角度测量中的可观察对象_Angular_Typescript_Ionic3_Observable_Google Cloud Firestore - Fatal编程技术网

Angular 使用观察者作为角度测量中的可观察对象

Angular 使用观察者作为角度测量中的可观察对象,angular,typescript,ionic3,observable,google-cloud-firestore,Angular,Typescript,Ionic3,Observable,Google Cloud Firestore,我只是对Angular的观察物和主体感到困惑。我已经搜索过类似的问题,但没有找到适合我需要的答案 假设我有一个observable类型变量,它监听Firestore中的数据更改并保存更改,如下所示: 导出类学术提供者{ 公共科学院:可观察; 私人db:任何; 构造函数(公共http:HttpClient, 私人afs:AngularFirestore, 私人帐户提供者:帐户提供者){ this.db=firebase.firestore(); } //此方法获取当前日志的详细信息 getCur

我只是对Angular的观察物和主体感到困惑。我已经搜索过类似的问题,但没有找到适合我需要的答案

假设我有一个observable类型变量,它监听Firestore中的数据更改并保存更改,如下所示:

导出类学术提供者{
公共科学院:可观察;
私人db:任何;
构造函数(公共http:HttpClient,
私人afs:AngularFirestore,
私人帐户提供者:帐户提供者){
this.db=firebase.firestore();
}
//此方法获取当前日志的详细信息
getCurrentAcademy(academyID):承诺{
返回新承诺((解决、拒绝)=>{
此.db.collection(“academy”).where(“id”,“==”,academyID).onSnapshot(
(学院)=>{
如果(!academy.empty){
this.currentAcademy=academy.docs[0].data();
console.log(this.currentAcademy);
解析(academy.docs[0].data());
}否则{
//学院并不存在
拒绝(假);
}
},
//学院错误
(错误)=>{
拒绝(错误);
})
});
}

}
使用
主题
如果您不想在初始化观察时获取值,如果您想在首次订阅观察者时接收值,请使用
行为主题

public currentAcademy: BehaviorSubject<any> = new BehaviorSubject(null);
public currentAcademy: Subject<any> = new Subject();
在您的组件中,您现在可以订阅此观察者:

this.myService.currentAcademy.subscribe(data => console.log(data));

它仍然不起作用,在组件中,它总是说数据没有定义。然后在获取数据时出现问题。不,我使用上述代码在提供程序中正确地获取数据。但我不能用您的代码将此数据传输到其他组件。很抱歉,我不相信您。这应该是有效的。如果没有,那么请用模拟的数据来证明。
this.myService.currentAcademy.subscribe(data => console.log(data));