1 回答
TA贡献1875条经验 获得超3个赞
你可以这样做
async function fetchWeatherr() {
try {
const response = await fetch(
'https://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=***'
);
const json = await response.json();
setData({ data: json });
setTemp({ temp: (json.main.temp - 273.15).toFixed(2) + ' C' });
setCityDisplay({ cityDisplay: json.name });
setIcon({ icon: json.weather[0].icon });
setMain({ main: json.weather[0].main });
setHumidity({ humidity: json.main.humidity + ' %' });
setPressure({ pressure: json.main.pressure + ' hPa' });
setVisibility({
visibility: (json.visibility / 1000).toFixed(2) + ' km',
});
} catch (err) {
console.warn('error');
}
}
添加回答
举报