获取未在Safari中定义(ReferenceError:找不到变量:fetch)

zlw*_*eld 21 javascript safari fetch-api

出于某种原因fetch(https://fetch.spec.whatwg.org/)未在Safari(版本9.0.3)中定义,有谁知道为什么?它似乎是标准,在Chrome和Firefox中运行良好.似乎找不到其他人有同样的问题

我正在使用与redux的反应,这是一些示例代码:

export function fetchData (url) {
  return dispatch => {
    dispatch(loading())
    fetch(url, {
      method: 'GET'
    })
    .then(response => {
      response.json()
      .then(data => {
        dispatch(success(data))
      })
    })
  }
}
Run Code Online (Sandbox Code Playgroud)

Dmi*_*riy 35

您可以将https://github.com/github/fetch polyfill用于不受支持的浏览器.

npm install whatwg-fetch --save; 
Run Code Online (Sandbox Code Playgroud)

编辑:(根据评论)

import 'whatwg-fetch'; 
Run Code Online (Sandbox Code Playgroud)

在使用fetch之前的每个文件中 - oliviergg

  • @nickvda尝试在使用fetch之前添加`import'whatwg-fetch';` (3认同)