javascript - 如何从 github api 输出存储库
问题描述
我只是在学习,请不要扔西红柿)情况如下,我使用其API在Github存储库上进行搜索,当用户打印某些内容时,他会显示可能的结果(如屏幕截图所示),现在的问题是,我如何遍历输入过程中出现的列表,以便按下时 showOnPage 函数适用于每个项目?
constructor() {
this.client_id = 'Iv1.46288115e5c16a2f';
this.client_secret = '25e67ed3b6da1ebf13b04de1a8a69bc227eb811f';
this.repo_count = 2;
}
async getRepo(userText){
const repoResponse = await fetch(`https://api.github.com/search/repositories?q=${userText}&client_id=${this.client_id}&client_secret=${this.client_secret}&per_page=${this.repo_count}`);
const repo = await repoResponse.json();
return{
repo: repo.items
}
}
}
class UI {
constructor() {
this.displayRepo = document.getElementById('displayRepo');
}
showRepo(repo){
let output = '';
repo.forEach(function (repo) {
output += `
<div class="item">${repo.name}</div>
`});
document.getElementById('displayRepo').innerHTML = output;
}
showOnPage(repo){ //<----------
let output = '';
repo.forEach(function (repo) {
output += `
<div class="repo-list-item">
<p class="p-list">Name: ${repo.name}</p>
<p class="p-list">Owner: ${repo.owner.login}</p>
<p class="p-list">Stars: ${repo.stargazers_count}</p>
<div class="close"></div>
</div>
`});
document.getElementById('displayRep').innerHTML = output;
}
clearProfile(){
this.displayRepo.innerHTML = '';
}
}
const gitRepo = new Github();
const ui = new UI();
const searchRepo = document.querySelector('.input-repo');//input
const form = document.querySelector(".autocomplete-list");//an output list when entering something into input
const item = document.querySelector(".item");//single item in list
searchRepo.addEventListener('keyup', e =>{
const userText = e.target.value;
if(userText !== ''){
gitRepo.getRepo(userText).then(data => {
ui.showRepo(data.repo);
});
}else{
ui.clearProfile();
}
});```
[1]: https://i.stack.imgur.com/aRFfW.png
解决方案
推荐阅读
- javascript - 在 reactJs 中使用 useState 更改嵌套对象中的值
- php - php -S 随机不工作:localhost 拒绝连接
- kubernetes - kubectl top no 不反映实际 RAM
- java - javax.crypto.AEADBadTagException
- c - 通过比较存储在每个节点中的字符串,按字母表对 LINKED LIST 中的节点进行排序。(C程序)
- javascript - HTML2canvas 屏幕截图无法离线工作
- arrays - 将字符串转换为所有大写字母并从函数中返回
- python - 发送带有数据框的电子邮件作为附件
- javascript - Material UI Select 组件使 React 应用程序崩溃
- android - 从微调器获取所选项目 - MVVM - 数据绑定