【问题标题】:'fetch' is not defined.eslint(no-undef) in react-native'fetch' 未定义.eslint(no-undef) 在 react-native
【发布时间】:2019-04-05 03:23:46
【问题描述】:

我在 react native 中使用 fetch 并有一条消息:'fetch' is not defined.eslint(no-undef)

我的代码:

getData() {
fetch('https://tutorialzine.com/misc/files/example.json', {})
  .then(res => res.json())
  .then((res) => {
    console.log(res);
  }).catch((e) => {
    console.log(e);
  });

}

我该如何解决?并且 console.log 不显示数据

【问题讨论】:

    标签: react-native fetch


    【解决方案1】:

    在 .eslintrc 文件中为浏览器设置环境:

    env:
      browser: true
    

    【讨论】:

      【解决方案2】:

      fetch 是浏览器环境中的全局方法。要静音 eslint 警告,你可以把它放在你的 eslint 配置文件中。

      "globals": {
        "fetch": false
      }
      

      这是来自eslint issue 的此答案的参考。

      【讨论】:

        猜你喜欢
        • 2018-07-13
        • 2017-10-29
        • 2023-03-12
        • 2021-02-28
        • 2020-12-21
        • 2019-11-10
        • 2019-10-05
        • 2018-03-18
        相关资源
        最近更新 更多