Why am I getting "ReferenceError: 'TextEncoder' is undefined" in IE11 after importing the text-encoding polyfill?
Why am I getting "ReferenceError: 'TextEncoder' is undefined" in IE11 after importing the text-encoding polyfill?
我正在尝试 pollyfill 库 fetch-readablestream (https://github.com/jonnyreeves/fetch-readablestream)。我已经添加了 polyfill,大多数事情都有效,但我不断收到有关 TextEncoder ERROR ReferenceError: 'TextEncoder' is undefined
的错误
我已经添加了所需的 pollyfill: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`.
我还尝试将脚本添加到 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 调用中看到这种情况。
使用 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';
!
通过在我的 index.html
header.
中包含此脚本,我能够填充 text-encoding
<script src="https://cdn.jsdelivr.net/npm/text-encoding@0.6.4/lib/encoding.min.js"></script>
我还没有尝试@iCrow 建议的方法。这可能是一个更好的解决方案。
感谢iCrow的回答,很有帮助。
我的项目是基于 Vue 的,我发现该网站在 IE Edge 44 上无法正常运行 运行。caniuse;
我按照iCrow的回答,把代码放到了一个文件中。
// text-encoding-polyfill.js
;(function (window) {
if (typeof window.TextEncoder !== 'function') {
const TextEncodingPolyfill = require('text-encoding');
window.TextEncoder = TextEncodingPolyfill.TextEncoder;
window.TextDecoder = TextEncodingPolyfill.TextDecoder;
}
}(window));
并将其导入到应用程序入口文件的顶部。
// main.js
import 'text-encoding-polyfill.js';
// ...
webpack打包代码后,IE Edge终于可以识别TextEncoder了!
我正在尝试 pollyfill 库 fetch-readablestream (https://github.com/jonnyreeves/fetch-readablestream)。我已经添加了 polyfill,大多数事情都有效,但我不断收到有关 TextEncoder ERROR ReferenceError: 'TextEncoder' is undefined
我已经添加了所需的 pollyfill: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`.
我还尝试将脚本添加到 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 调用中看到这种情况。
使用 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';
!
通过在我的 index.html
header.
<script src="https://cdn.jsdelivr.net/npm/text-encoding@0.6.4/lib/encoding.min.js"></script>
我还没有尝试@iCrow 建议的方法。这可能是一个更好的解决方案。
感谢iCrow的回答,很有帮助。 我的项目是基于 Vue 的,我发现该网站在 IE Edge 44 上无法正常运行 运行。caniuse;
我按照iCrow的回答,把代码放到了一个文件中。
// text-encoding-polyfill.js
;(function (window) {
if (typeof window.TextEncoder !== 'function') {
const TextEncodingPolyfill = require('text-encoding');
window.TextEncoder = TextEncodingPolyfill.TextEncoder;
window.TextDecoder = TextEncodingPolyfill.TextDecoder;
}
}(window));
并将其导入到应用程序入口文件的顶部。
// main.js
import 'text-encoding-polyfill.js';
// ...
webpack打包代码后,IE Edge终于可以识别TextEncoder了!