我遇到一个问题,我无法让观察者返回我在其中运行的过滤集。我从Giftler(https://www.nativescript.org/blog/merry-and-bright-create-a-mobile-app-with-firebase-angular-2-and-nativescript)示例完全从头到尾复制了它,它在那里工作,但似乎不适合我..
这是我的代码与问题相关
这是数组信息
beers: BehaviorSubject<Array<Beer>> = new BehaviorSubject([]);
private _allBeers: Array<Beer> = [];
这是我获取本地啤酒信息的功能(最初使用firebase加载数组中的数据)
getLocalBeerDetail(id: string): Observable<any> {
return new Observable((observer: any) => {
observer.next(
this._allBeers.filter(s => s.id === id)[0]
);
console.log("Here is the local query status of allBeers array: " + this._allBeers);
}).share();
}
这是调用上述函数并进行订阅的函数。
ngOnInit() {
this.sub = this.route.params.subscribe((params: any) => {
this.id = params['id'];
this.firebaseService.getLocalBeerDetail(this.id).subscribe((beer) => {
console.log("Here is the status of beer observable: " + beer);
this.ngZone.run(() => {
for (let prop in beer) {
if (prop === "id") {
this.id = beer[prop];
console.log("Here is the detail id:" + this.id);
}
if (prop === "name") {
this.name = beer[prop];
console.log("Here is the detail id:" + this.name);
}
}
});
});
});
}
根据使用Giftler示例,不确定问题是什么,但这应该有效。