Babel 不转换获取 API 代码



fetch api真的很有帮助,但不幸的是,它不适用于大多数浏览器,尤其是Internet Explorer。我尝试使用 babel 将我的代码从 es6 转换为 es5,但它没有解决这个问题。当它转换为 es5 时,它仍然包括 fetch。我怎样才能解决这个问题。这是 es6 代码:

var btnText = document.getElementById('btnText');
var btnJson = document.getElementById('btnJson');
btnText.addEventListener("click",fetchBtnText);
function fetchBtnText() {
  fetch("sample.txt")
    .then((response) => response.text())
.then((data) => console.log(data))
}

这是到 es5 的转换

'use strict';
var btnText = document.getElementById('btnText');
var btnJson = document.getElementById('btnJson');
btnText.addEventListener("click", fetchBtnText);
function fetchBtnText() {
  fetch("sample.txt").then(function (response) {
    return response.text();
  }).then(function (data) {
    return console.log(data);
  });
}

你可以使用polyfill,像这样https://github.com/github/fetch

最新更新