Angular 从行为主体中观察到的行为不正常

Angular 从行为主体中观察到的行为不正常,angular,rxjs,Angular,Rxjs,我正在开发一个Angular应用程序,我正在尝试使用一个带有行为主题的服务,该主题正在从firestore获得更新。我正在尝试创建一个函数,该函数将返回一个只包含选定项的可观察项。我的问题是,当我订阅从行为主体创建的可观察对象时,可观察对象发出一个空数组,并且在数据更改时不再触发 我的服务是这样的 export class TripService { constructor(private db: AngularFirestore, private store: Store<fr

我正在开发一个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])
            ))
        )
    }
}
ngOnInit() {
    this.tripService.getSelectedTrip().pipe(
        tap((trip: Trip) => this.trip = trip)
    .subscribe(trip => console.log(trip))  //logs: [] and doesn't emit again
}
我的物品组件是这样的

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])
            ))
        )
    }
}
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中没有

你能提供stackblitz吗?示例演示会有所帮助。您可以模拟firestore响应。在旁注上-您可能不想使用。点击下一步。Tap主要用于副作用。您可以直接映射到tripSource流