【发布时间】:2020-03-05 17:32:56
【问题描述】:
我是 Typescript React 和 Ionic 框架的新手。
我正在使用 JS FETCH API 并从第三方获取数据。现在,我想获取获取的数据并将其返回到我的 DOM,但我不知道如何在我的 Fetch 之外访问获取的数据。
请赐教..谢谢
function getData() {
fetch('URL')
.then(response => {
return response.json();
}).then(data_from_fetched => {
let data = data_from_fetched.results;
return data;
}}
let data = getData()
console.log(data); //undefined
让数据 = getData() 控制台.log(数据); //未定义
我也试过这个异步..
async function getData() {
fetch('url')
.then(response => {
return response.json();
}).then(data_from_fetched => {
console.log(data_from_fetched)
let data = data_from_fetched.results;
return data;
})
}
getData().then(data => console.log(data));
或者这个..
function getData() {
fetch('url')
.then(response => {
return response.json();
}).then(data_from_fetched => {
console.log(data_from_fetched)
let data = data_from_fetched.results;
return data;
})
}
let data= getData();
// 并将其显示在我的 DOM 上
{movies.map(movie => movie){
let title = <IonCardTitle>{movie.title}</IonCardTitle>}}
【问题讨论】:
标签: javascript reactjs typescript ionic-framework