【发布时间】:2021-10-05 11:30:34
【问题描述】:
所以我有一个 Pokemon API 并通过 .fetch() 构建了前端,我想创建另一个模式来显示代码一些统计信息。我已经获得了要连接的 Bootstrap 模式,但我无法从 API 获取数据以进入第二个模式。似乎我的代码根本没有与第二个模态连接。这是一个代码笔,因为这个项目太大,无法在此处发布。
API 链接: https://pokeapi.co/api/v2/pokemon/?limit=150
代码笔: https://codepen.io/drxl/pen/OJmZJQZ
这是我试过的JS:
//loads the stats for 2nd modal
function loadStats(item) {
let url = item.detailsUrl;
return fetch(url).then(function (response) {
return response.json();
}).then(function (details) {
//add details to stats
item.stats = [];
for (var i = 0; i < details.stats.length; i++) {
item.stats.push(details.stats[i].stat.name);
}
item.stats = item.stats.join('<br>');
}).catch(function (e) {
console.error(e);
});
}
function loadStatDetails(item) {
pokemonRepository.loadStats(item).then(function () {
showStatModal(item);
});
}
function showStatModal(item) {
pokemonRepository.loadStats(item).then(function () {
// eslint-disable-next-line no-undef
let StatmodalBody = $(".Statmodal-body");
// eslint-disable-next-line no-undef
let StatmodalTitle = $(".Statmodal-title");
//clears previous content in modal
StatmodalTitle.empty();
StatmodalBody.empty();
//create elenebtb for pokemon name
// eslint-disable-next-line no-undef
let nameElement = $("<h1>" + item.name + "</h1>");
//add stats
let statsElement = $("<p>" + item.stats + "<p>");
StatmodalTitle.append(nameElement);
StatmodalBody.append(statsElement);
$('#my-Statmodal').modal('show');
});
}
return {
add: add,
getAll: getAll,
addListItem: addListItem,
loadList: loadList,
loadDetails: loadDetails,
showDetails: showDetails,
loadStats: loadStats,
loadStatDetails: loadStatDetails,
};
【问题讨论】:
-
什么在调用
loadStatDetails函数?为什么调用pokemonRepository.loadStats(item)两次? -
我不知道我是菜鸟
-
检查下面的asnwer,它现在在第二个模态中加载统计数据
-
不用担心@NamelessDude21。我们都是从某个时候开始的。
标签: javascript jquery bootstrap-modal fetch-api bootstrap-5