导入文本编码的polyfill后,为什么在IE11中出现“ ReferenceError:'TextEncoder'未定义?”

问题描述 投票:6回答:1

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

的错误

我已经添加了必需的pollyfills:web-streams-polyfill,文本编码和babel-polyfill。我尝试了这些polyfill的其他等效方法,但遇到了同样的问题。

我未注释的所需IE导入后的[我的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

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

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

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调用中看到这种情况。

angular polyfills
1个回答
0
投票

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

然后添加到polyfills.ts

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

请勿使用import 'text-encoding';

© www.soinside.com 2019 - 2024. All rights reserved.