1.問題が発生する
バージョンの問題により、プロジェクトはstomp.jsをアップグレードする必要があります。ただし、stomp.jsパッケージ内で使用されるTextEncoderおよびTextDecoderは、IEでは互換性がありません。その結果、プロジェクト全体をIEで実行することはできません。互換性チェックエントリ。
二、解決しよう
text-encoding.js
私は、これはgithubのので、NPMに互換ライブラリテキストencoding.jsを発見したリンクアドレス、およびこれはNPMあるリンクアドレス。残念なことに、これらは両方とも維持されなくなり、同社はそれらをオープンソースソフトウェアとして紹介することができなかったため、あきらめました。もちろん、全員の要件がそれほど厳しくない場合に使用できます。
text-encoding.jsを自分で書く
Stomp.jsのソースコードを調べると、TextEncoderはIEでは互換性がありませんが、TextEncoderの最も基本的なエンコーディング、つまりutf-8のみが使用されます。
//--TextEncoder源码部分
// 2. Set enc's encoding to UTF-8's encoder.
if (Boolean(options['NONSTANDARD_allowLegacyEncoding'])) {
// NONSTANDARD behavior.
label = label !== undefined ? String(label) : DEFAULT_ENCODING;
let encoding = getEncoding(label);
if (encoding === null || encoding.name === 'replacement') {
throw RangeError('Unknown encoding: ' + label);}
if (!encoders[encoding.name]) {
throw Error('Encoder not present.' +
' Did you forget to include encoding-indexes.js first?');
}
enc._encoding = encoding;
}
その中には、DEFAULT_ENCODING
utf-8エンコーディングがあります。utf-8エンコーディングの場合のみ、ブラウザー互換性unescape
とencodeURIComponent
連携を使用して以下をシミュレートできます。
var encoder = new TextEncoder();
encoder.encode("中文abc");
//result : Uint8Array(9) [228, 184, 173, 230, 150, 135, 97, 98, 99]
unescape(encodeURIComponent("中文abc")).split("").map(val => val.charCodeAt());
//result : (9) [228, 184, 173, 230, 150, 135, 97, 98, 99]
同様に、デコードは次のとおりです。
var decoder = new TextDecoder();
decoder.decode(Uint8Array.from([228, 184, 173, 230, 150, 135, 97, 98, 99]));
//result : 中文abc
decodeURIComponent(escape(String.fromCharCode(...[228, 184, 173, 230, 150, 135, 97, 98, 99])));
//result : 中文abc
したがって、IEと互換性を持たせるために、ES5パッケージは次のようになります。
/**
* @description 这是一个补丁包。用来解决新引入的stomp中引用textEncoder和textDecoder在IE下不兼容的问题
* 由于stomp源码中只使用了最基本的utf8编码,故可以用支持ie的 unescape 和 encodeURIComponent伪装该函数
* @param {type}
* @Date 2020-07-08 11:45:19
*/
(function(window) {
if(undefined !== window.TextEncoder) {
return;}
function _TextEncoder() {
//--DO NOTHING
}
_TextEncoder.prototype.encode = function(s) {
return unescape(encodeURIComponent(s)).split('').map(function(val) {
return val.charCodeAt();});
};
function _TextDecoder() {
//--DO NOTHING
}
_TextDecoder.prototype.decode = function(code_arr) {
return decodeURIComponent(escape(String.fromCharCode.apply(null, code_arr)));
};
window.TextEncoder = _TextEncoder;
window.TextDecoder = _TextDecoder;
})(this);
直接引用してください。