获取types错误:无法在“窗口”上执行“获取”:非法调用

我试图使用fetch调用后端,而不需要像Axios这样的库。

api.ts

export const sendSuggestion = ((data: any): Promise<any> => { console.log(JSON.stringify(data)); const apiUrl = `/api/suggest/`; return fetch(apiUrl, { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(data) }).then(checkStatus) .then(r => r.json()) .then(data => { console.log(data); }); }); const checkStatus = ((response: any) => { if (response.ok) { return response; } else { var error = new Error(response.statusText); console.log(error); //return Promise.reject(error); } }) 

另外,我包括npm模块是polyfill https://www.npmjs.com/package/unfetch并将其导入我的代码

  import fetch from 'unfetch' console.log(fetch) returns in console: function fetch() { [native code] } 

我不能理解这个问题。

使用unfetch你可以做:

const fetch = unfetch.bind();

如果你想使用窗口:

const fetch = window.fetch.bind(window);