我正在使用Angular应用程序,在该应用程序中,我尝试使用行为主题从Firestore获取更新的服务。我正在尝试制作一个仅返回选定项目即可返回可观察值的函数。我的问题是,当我订阅从行为主题创建的可观察对象时,该可观察对象发出一个空数组,并且在数据更改时不再触发。
我的服务看起来像这样
export class TripService {
constructor(private db: AngularFirestore, private store: Store<fromRoot.State>) { }
private tripSource = new BehaviorSubject([])
public trips$: Observable<any> = this.tripSource.asObservable()
updateTrips() {
return this.db.collection('trips', ref => ref.where('agentId', '==', authState.uid).valueChanges({ idField: 'id' })).pipe(
tap((trips) => this.tripSource.next(trips))
)
}
getSelectedTrip() {
return this.store.select(fromRoot.getTripId).pipe( //using ngrx to get selected trip
filter((tripId: any) => !!tripId),
switchMap(tripId => this.trips$.pipe(
map((trips: Trip[]) => trips.filter(trip => trip.id === tripId)[0])
))
)
}
}
在我的app.component中,我正在调用updateTrips函数以获取初始数据,并且只要Firestore中的数据发生更改,它就会将新行程注销到控制台
ngOnInit() {
this.store.dispatch(fromAuthActions.loadUser());
this.tripService.updateActiveAgentTrips().subscribe(trips => console.log('trips are:', trips)); //this logs the trips!
this.clientService.getAgentClients().subscribe()
this.destinationService.updateDestinations().subscribe();
}
我的项目组件ngOninit看起来像这样
ngOnInit() {
this.tripService.getSelectedTrip().pipe(
tap((trip: Trip) => this.trip = trip)
.subscribe(trip => console.log(trip)) //logs: [] and doesn't emit again
}
当Firestore中的数据发生更改时,是否有理由在app.component中发出可观察到的消息,而不在trip.component中发出?