2

我正在使用 NGRX 商店开发 Angular 7 应用程序。我在 Chrome 浏览器中安装了Redux DevTools,以便在开发过程中使用它。

当我注意到一个函数的行为不同时,我感到非常困惑,这取决于 Redux DevTools 在浏览器中是启用还是禁用。在一种情况下,我遇到了错误,在另一种情况下 - 一切正常。

确切的错误是:

TypeError: You provided an invalid object where a stream was expected. 
You can provide an Observable, Promise, Array, or Iterable 

幸运的是,我在这个答案中找到了问题的根源——switchMap由于 WebStorm 的自动导入,我错误地导入了:

import { switchMap } from 'rxjs/internal/operators';

代替

import { switchMap } from 'rxjs/operators';

但是,我仍然无法理解 Redux DevTools 与它有什么关系,以及为什么我的代码在switchMap启用该工具时就像我正确导入一样工作,反之亦然 - 它导致错误(因为它通常应该有一直在做)当它被禁用时。

我怀疑这段代码在这里会有很大帮助,但它是实际错误发生的地方。

return this._http
            .post(url, body, { headers })
            .pipe(
                // This switch map and the throwError function were causing the error.
                switchMap((response: any) => {
                    // Please, do ignore the logic itself. There's a reason behind it.
                    const { data } = response;
                    return !!data ? of(data) : throwError('Invalid arguments');
                })
            );
4

0 回答 0