我正在通过HttpClient加载数据并将其发布为:
export interface Car {
id:number;
make: string;
status: number;
}
private cars = new BehaviorSubject<Car[]>([]);
constructor( private http: HttpClient) {
this.loadCars();
}
loadCars() {
this.http.get<Car[]>('/api/cars')
.subscribe((cars) => this.cars.next(cars));
}
getFilteredCars() {
return this.cars.asObservable()
.pipe(
map( (cars) => cars.filter( car => car.status === 1)
)
);
}
如你所见,我想得到所有状态等于1的车。
当我从我的组件调用它时,我什么都没有得到:
Component
cars: Observable<Car[]>;
constructor(private carService: CarService) {
this.cars = carService.getFilteredCars();
}
如果返回未过滤的主题,我将毫无问题地获取未过滤的数据:
getFilteredCars() {
return this.cars.asObservable();
}
我做错什么了?