Angular 5等待服务调用完成有效

时间:2018-06-01 16:28:14

标签: ngrx-effects

一旦我们获得成功响应以及数据,那么只应该从效果中触发操作。但是每次都没有设置,即使http调用成功,大部分时间都是null。

@Effect()
setEmp = this.actions$
  .ofType(AuthActions.TRY_SET_EMP)
  .switchMap(async (action: AuthActions.SetEmp) => {
      return await zip(this.getUserModelObservable());
  }).mergeMap((user, index) => {
    return [ //it should be called only when success response along with data is returned from getUserModelObservable()

      {
        type: AuthActions.SET_EMP_IN_STORE,
        payload: user[index]
      }
    ];
  });

getUserModelObservable(): Observable<EmpModel> {
    const path = `${environment.api_url}/emp/me`;
    const headers = {
        'x-cacheable' : 'true',
        'x-refresh' : 'false'
    };
    return this._http.get(path, {headers: headers}).retry(2).catch(error => {
        return Observable.throw(error.message || error);
    }) as Observable<EmpModel>;
}

0 个答案:

没有答案