在我的angular应用程序中使用ngrx效果替换我的服务时缺少我的下一个()

在我的angular应用程序中使用ngrx效果替换我的服务时缺少我的下一个(),angular,typescript,rxjs,ngrx,Angular,Typescript,Rxjs,Ngrx,在我的应用程序中,我从web api获取数据。我有我的服务,看起来像这样: getVehicles(): Observable<Vehicle[]> { return this.http.get<Vehicle[]>(this.API_URL).pipe( tap(() => this.log(`fetched vehicles`)), catchError(this.handleError('getVehicles', []))

在我的应用程序中,我从web api获取数据。我有我的服务,看起来像这样:

getVehicles(): Observable<Vehicle[]> {
    return this.http.get<Vehicle[]>(this.API_URL).pipe(
      tap(() => this.log(`fetched vehicles`)),
      catchError(this.handleError('getVehicles', []))
    );
  }
但是现在我使用的是ngrx,我的代码看起来很不一样,原因很明显

  ngOnInit() {
    // getvehicles
    this.vehicles$ = this.store.select(fromStore.getAllVehicles);
    this.store.dispatch(new fromStore.LoadVehicles());
}
问题是,我过去可以在ngOnInit中使用next()方法,使用函数setMarkers()在地图上设置标记。当我在旧的getVehicles函数外调用my setMarkers()时,它通常不返回任何内容,因为在应用程序初始化的最初几秒钟内,我的车辆是未定义的。通过在下一个方法中使用它,我可以避免这种情况。
然而,情况已经不是这样了,我想知道,现在当我更改代码时,我是否可以做类似的事情来使用我的函数setMarkers()?

对于任何感兴趣的人,我通过订阅存储,在我的ngOnInit中,并在调用数据时发送来解决这个问题(例如在“我的更新”按钮功能上)


您是否在代码中的某个地方订阅了
this.vehicles$
?我认为您需要使用
this.vehicles$.subscribe(v=>…)
获取车辆的更新状态我现在不订阅它,但我认为使用ngrx的一个重要原因是避免使用我以前使用的旧方法?这就是为什么我在寻找另一种方法。但是我不太确定!@maac
ngrx
不会让你订阅观测值。@Envil我明白了。但是en是订阅我的观测值的合适时间吗?当我在开始时设置我的车辆$时,我需要时间才能订阅它。如果我尝试在ngOnInit中订阅我的观测值,它将尝试订阅一些未定义的内容。@maac你仍然可以在
ngOnInit
中订阅,但你可以在那里放置一个过滤器来过滤掉e
未定义的
结果。
this.getVehicleDetails().subscribe(data => {
        this.vehicleDetails = data;
        this.setMarkers();
      })
  ngOnInit() {
    // getvehicles
    this.vehicles$ = this.store.select(fromStore.getAllVehicles);
    this.store.dispatch(new fromStore.LoadVehicles());
}
// init selected vehicle details inside of my ngOnInit
ngOnInit() {
    this.store.select(fromStore.getAllVehicleDetail).subscribe(data => {
      this.vehicleDetail = data;
      if (this.vehicleDetail.position) {
        this.setMap();
        this.setMarkers();
      } else {
        this.failSetMap();
        this.setMarkers();
      }
    });
}


click() {
     this.store.dispatch(new fromStore.LoadVehicleDetail()); 
  }