在本地测试时,浏览器无法识别“fetch”函数。需要使用一个Polyfill库来模拟“fetch”函数的常见请求和响应功能。可以使用“whatwg-fetch”库,在代码中导入它,然后使用它来获取远程数据。示例代码如下:
import 'whatwg-fetch';
// 在这里使用“fetch”函数获取远程数据
fetch('https://api.example.com/data')
.then(response => {
if (response.ok) {
return response.json();
} else {
throw new Error('Network response was not ok.');
}
})
.then(data => {
console.log(data);
})
.catch(error => {
console.error('There was a problem with the fetch request:', error);
});