【问题标题】:Angular 11 - TypeError: Failed to execute 'fetch' on 'Window': Illegal invocationAngular 11-TypeError:无法在“窗口”上执行“获取”:非法调用
【发布时间】:2021-04-20 15:08:33
【问题描述】:

我正在使用一个依赖于 isomorphic-unfetch 的依赖项。 在 Angular 11 项目中,我可以执行 isomorphic-unfetch 本身而不会出现任何错误。 当使用内部使用 isomorphic-unfetch 来获取的依赖项(通过 npm 安装)时,我收到以下消息:

index.js:209 TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation
at zone-evergreen.js:1465
at Object.proto.<computed> [as default] (zone-evergreen.js:975)
at index.js:189
at step (index.js:70)
at Object.next (index.js:51)
at index.js:44
at new ZoneAwarePromise (zone-evergreen.js:1387)
at __awaiter (index.js:40)
at index.js:380

当集成到 React 或纯 JS/TS 中时,依赖项工作正常。 我在这里发现了一些相关的问题,这些问题并没有专门针对 Angular。

【问题讨论】:

    标签: angular typescript npm fetch isomorphic-fetch-api


    【解决方案1】:

    由于 Angular 提供了自己的库来发出 http 请求 HttpClientModuleHttpClient,因此通常不需要像 unfetch 等第三方库。这就是没有人在文档或其他地方定位 Angular 的原因。

    但是应该仍然可以使用该库。在您的情况下,我能想到的一些提示出错了:

    • Window 使用大写字母 W 而不是 w
    • 也许您需要初始化一些对等依赖项,例如 import 'unfetch' 以运行它们的默认脚本(然后将 fetch 函数添加到窗口对象)

    【讨论】:

      猜你喜欢
      • 2021-04-26
      • 2018-08-13
      • 1970-01-01
      • 2019-10-13
      • 2021-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-29
      相关资源
      最近更新 更多