为什么我会收到"ReferenceError:"TextEncoder"未定义"在IE11中导入文本编码的polyfill后? [英] Why am I getting "ReferenceError: 'TextEncoder' is undefined" in IE11 after importing the text-encoding polyfill?

查看:118
本文介绍了为什么我会收到"ReferenceError:"TextEncoder"未定义"在IE11中导入文本编码的polyfill后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对库fetch-可读流(https://github.com/jonnyreeves/fetch-可读流).我已经添加了polyfills,并且大多数事情都能正常工作,但是我不断收到有关TextEncoder的错误 ERROR ReferenceError:'TextEncoder'未定义

I am trying to pollyfill a library fetch-readablestream (https://github.com/jonnyreeves/fetch-readablestream). I have added the polyfills and most things do work but I keep getting an error about TextEncoder ERROR ReferenceError: 'TextEncoder' is undefined

我已经添加了必需的pollyfills:web-streams-polyfill,text-encoding和babel-polyfill.我尝试了其他等效的这些polyfills,但遇到了同样的问题.

I've added the required pollyfills: web-streams-polyfill, text-encoding, and babel-polyfill. I tried other equivalents of these polyfills but I get the same issue.

我的 polyfills.ts 文件.

import 'web-streams-polyfill'; // Run `npm install --save web-streams-polyfill`.
import 'text-encoding'; // Run `npm install --save text-encoding`.
import 'babel-polyfill'; // Run `npm install --save babel-polyfill`.

我还尝试将脚本添加到 index.html

I also tried adding scripts to index.html

<script src="node_modules/text-encoding/lib/encoding-indexes.js"></script>
<script src="node_modules/text-encoding/lib/encoding.js"></script>

我不希望有任何错误,但是我得到了:

I don't expect any errors but I get this:

ERROR ReferenceError: 'TextEncoder' is undefined
   "ERROR"
   {
      [functions]: ,
      __proto__: { },
      description: "'TextEncoder' is undefined",
      message: "'TextEncoder' is undefined",
      name: "ReferenceError",
      number: -2146823279,
      stack: "ReferenceError: 'TextEncoder' is undefined
   at responseParserFactory (http://localhost:4200/vendor.js:139703:3)
   at xhrTransport (http://localhost:4200/vendor.js:139960:1)
   at fetchStream (http://localhost:4200/vendor.js:139795:4)
   at DevicewiseService.prototype.getNotifications (http://localhost:4200/main.js:6577:9)
   at Anonymous function (http://localhost:4200/main.js:159:17)
   at SafeSubscriber.prototype.__tryOrUnsub (http://localhost:4200/vendor.js:145834:9)
   at SafeSubscriber.prototype.next (http://localhost:4200/vendor.js:145772:13)
   at Subscriber.prototype._next (http://localhost:4200/vendor.js:145715:5)
   at Subscriber.prototype.next (http://localhost:4200/vendor.js:145692:9)
   at MapSubscriber.prototype._next (http://localhost:4200/vendor.js:150984:5)",
      Symbol([[Cancel]])_g.r6fxkqwxet3: undefined,
      Symbol([[Pull]])_h.r6fxkqwxet3: undefined,
      Symbol(INITIAL_VALUE)_p.r6fxkqwxet3: undefined,
      Symbol(rxSubscriber)_o.r6fxkqwxet3: undefined
   }

我们可以在fetchStream调用中看到这种情况.

We can see this happens in the fetchStream call.

推荐答案

使用npm软件包: https://www.npmjs.com/package/text-encoding (现已不推荐使用,但就像一个超级按钮一样工作.)

Use npm package: https://www.npmjs.com/package/text-encoding (now deprecated, but works like a charm).

然后添加到 polyfills.ts 代码:

if (typeof window['TextEncoder'] !== 'function') {
  const TextEncodingPolyfill = require('text-encoding');
  window['TextEncoder'] = TextEncodingPolyfill.TextEncoder;
  window['TextDecoder'] = TextEncodingPolyfill.TextDecoder;
}

请勿使用 import'text-encoding';

这篇关于为什么我会收到"ReferenceError:"TextEncoder"未定义"在IE11中导入文本编码的polyfill后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆