如何使箭头功能适用于IOS 9.3.1?

时间:2016-05-08 03:44:28

标签: javascript ios babel

我喜欢使用fetch API& ES6箭头功能,但我很难得到一个简单的例子来处理IOS Safari 9.3.1。



function foobar() {
  fetch("https://httpbin.org/get", {
      method: "GET"
    })
    .catch(() => {
      console.log("Fail zone");
    })
    .then((res) => {
      if (res.ok) {
        res.json().then((json) => {
          document.getElementById("response").innerHTML = JSON.stringify(json, null, 2);
        });
      } else {
        console.log("error", res);
      }
    });
}

<button onClick="foobar(this);">Just testing</button>
<pre id=response></pre>
&#13;
&#13;
&#13;

我错过了什么Javascript以使其在IOS中运行?没有热衷于创建我自己的bundle.js。

当我使用fetch polyfill&amp;在https://react.dabase.com/foo/ type="text/babel" {}获得 ReferenceError:在Safari的检查员中无法找到变量

此外,我在Chrome中获得未捕获的ReferenceError:foobar未定义。我错过了什么?

1 个答案:

答案 0 :(得分:3)

为了使箭头功能正常工作,我安装了npm install babel-cli babel-preset-es2015&amp;然后跑了:

./node_modules/babel-cli/bin/babel.js --presets es2015 main.js -o compiled.js

对于IOS中的Fetch API支持,我刚刚采购https://cdnjs.cloudflare.com/ajax/libs/fetch/1.0.0/fetch.min.js

如果有更好的方法,请告诉我。