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

Aus*_*one 7 polyfills angular

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

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

polyfills.ts所需要的IE进口,我注释掉后的文件。

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`.
Run Code Online (Sandbox Code Playgroud)

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

<script src="node_modules/text-encoding/lib/encoding-indexes.js"></script>
<script src="node_modules/text-encoding/lib/encoding.js"></script>
Run Code Online (Sandbox Code Playgroud)

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

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
   }
Run Code Online (Sandbox Code Playgroud)

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

小智 7

使用 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;
}
Run Code Online (Sandbox Code Playgroud)

不要使用 import 'text-encoding';