PolyDeill for TextDecoder

时间:2016-11-17 18:12:40

标签: javascript fetch polyfills

我正在使用fetch,并在我的应用程序中包含了whatwg-fetch polyfill

我还使用了Jake Archibald博客TextDecoder中描述的That's so fetch!来解码响应,但我不确定要使用哪种polyfill。

(目前Safari抱怨ReferenceError: Can't find variable: TextDecoder

我猜测TextDecoder有一个polyfill,但我找不到它......

4 个答案:

答案 0 :(得分:9)

我能够使用text-encoding

来解决这个问题
npm install text-encoding --save

一起
import encoding from 'text-encoding';
const decoder = new encoding.TextDecoder();

答案 1 :(得分:5)

用于快速客户端测试(不使用NPM):

in_addr_t

..然后像正常一样使用<script src="https://unpkg.com/text-encoding@0.6.4/lib/encoding-indexes.js"></script> <script src="https://unpkg.com/text-encoding@0.6.4/lib/encoding.js"></script> MDN's example:

TextDecoder
var win1251decoder = new TextDecoder('windows-1251');
var bytes = new Uint8Array([207, 240, 232, 226, 229, 242, 44, 32, 236, 232, 240, 33]);
console.log(win1251decoder.decode(bytes)); // Привет, мир!

答案 2 :(得分:0)

如今,您可以按照FastestSmallestTextEncoderDecoder的建议使用MDN website填充(1.5 KB)。

答案 3 :(得分:0)

如果只需要将数组解码为utf8,则只需添加一个函数,而无需外部库:

set()