ngrx效果赋予类型'void'不能分配给'ObservableInput'类型

问题描述 投票:2回答:1
@Injectable()
export class ApiSearchEffects {

  @Effect()
  search$: Observable<any>
    = this.actions$
    .ofType(query.ActionTypes.QUERYSERVER)
    .debounceTime(300)
    .map((action: query.QueryServerAction) => action.payload)
    .switchMap(payload => {
      console.log(payload);
      const nextSearch$ = this.actions$.ofType(query.ActionTypes.QUERYSERVER).skip(1);

      this.searchService.getsearchresults(payload)
        .takeUntil(nextSearch$)
        .map((response) =>
          ({type: '[Search] Change', payload: response}
          ))
    });

上面的代码给出了类型'(payload:any)=> void'的参数不能赋值给'(value:any,index:number)=> ObservableInput'类型的参数。类型'void'不能分配给'ObservableInput'类型。哪里可能是错误的。我跟随https://github.com/ngrx/effects/blob/master/docs/intro.md的ngrx特效官方介绍。

angular rxjs ngrx ngrx-effects ngrx-store
1个回答
4
投票

问题是你的switchMap应该返回一个值;你有它的回归空虚。

试试这个

@Injectable()
export class ApiSearchEffects {

  @Effect()
  search$: Observable<any>
    = this.actions$
    .ofType(query.ActionTypes.QUERYSERVER)
    .debounceTime(300)
    .map((action: query.QueryServerAction) => action.payload)
    .switchMap(payload => {
      console.log(payload);
      const nextSearch$ = this.actions$.ofType(query.ActionTypes.QUERYSERVER).skip(1);

  return this.searchService.getsearchresults(payload)
        .takeUntil(nextSearch$)
        .map((response) =>
          ({type: '[Search] Change', payload: response}
          ))
    });
© www.soinside.com 2019 - 2024. All rights reserved.