Ngrx条件swtichMap调度多个动作

时间:2019-01-17 10:50:17

标签: angular rxjs ngrx

Webstorm给我一个错误:

  

TS2345:类型的参数(res:任意)=>属性“ type”的类型不兼容。不能将类型“ CLOSE_ACTIVE_CHATS”分配给类型“ DELETE_SELECTED_CHAT”。

在我内部的switchMap上

switchMap((res) => {//error here
  chat.mdRef.close();
  if (chat.chat.state === 2) {
    return [new CloseOpenChat(chat.chat._id), new 
      DeleteSelectedChat(), 
      new AddClosedChat(chat.chat)];
  })

完整代码

@Effect()
closeChat: Observable<any> = this.actions.pipe(
  ofType(CLOSE_CHAT_CALL),
  map((action: _Actions.AddClosedChat) => {
    return action.payload;
  }),
  switchMap((chat: CloseChatI) => {
    return this.messageService.closeChat(chat.chat._id).pipe(
      // @ts-ignore
      switchMap((res) => {
        chat.mdRef.close();
        if (chat.chat.state === 2) {
          return [new CloseOpenChat(chat.chat._id), new 
            DeleteSelectedChat(), 
            new AddClosedChat(chat.chat)];
        }
        return [new CloseActiveChat(chat.chat._id), new 
          DeleteSelectedChat(), 
          new AddClosedChat(chat.chat)];
      }),
      catchError(ErrorService.handleError)
    );
  }),
  catchError((err) => of(new CallFailed())
  )
);

1 个答案:

答案 0 :(得分:0)

DeleteSelectedChat()似乎还分派了一个动作,导致分派了多个动作。

如果您将DeleteSelectedChat()注释掉,您是否还会收到错误消息?