How to fetch GitHub API with vuejs

I'm a Software Engineer, studied Software Engineering at AltSchool of Africa, and computer science at the University of Technology. I'm passionate about sharing knowledge, documentation, web engineering, and others. I was my Team Lead at AltSchool Africa School of Engineering and I promote admirable leadership.
https://bob-adoga-git-hub-ap-ifetchexam.vercel.app/
Set up a Vue.js project
To create a new Vue.js project, you can use the Vue CLI command line interface
Install Vue CLI by running the following command in your terminal:
npm install -g @vue/cli
Create a new Vue.js project by running the following command;
vue create my-project
- Follow the prompts to choose the options for your project (e.g., Babel, ESLint, etc.)
Create a component to fetch and display repositories
Create a new component file (e.g.,
Repositories.vue) in thesrc/componentsdirectoryIn the component file, define a
dataobject with an empty array for the repositories, and acurrentPagevariable to keep track of the current page number
<template>
<div>
<div v-for="repo in repositories" :key="repo.id">
<h2>{{ repo.name }}</h2>
<p>{{ repo.description }}</p>
<p>Language: {{ repo.language }}</p>
</div>
<button @click="loadMore">Load more</button>
</div>
</template>
<script>
export default {
data() {
return {
repositories: [],
currentPage: 1,
};
},
methods: {
loadMore() {
this.currentPage++;
this.fetchRepositories();
},
fetchRepositories() {
const accessToken = "YOUR_ACCESS_TOKEN";
const username = "octocat";
const perPage = 10;
const url = `https://api.github.com/users/${username}/repos?per_page=${perPage}&page=${this.currentPage}`;
fetch(url, {
headers: {
Authorization: `Bearer ${accessToken}`,
},
})
.then((response) => response.json())
.then((data) => {
this.repositories.push(...data);
});
},
},
mounted() {
this.fetchRepositories();
},
};
</script>
In the
fetchRepositoriesmethod, we usefetch()to make an API request to the GitHub API, passing in the current page number and number of items per pageWe then append the fetched repositories to the
repositoriesarray using the spread operatorWe also define a
loadMoremethod that increments thecurrentPagevariable and callsfetchRepositoriesagain to load the next page of repositoriesFinally, we call
fetchRepositoriesin themountedhook to load the initial page of repositories
Use the component in your Vue.js app
- In your main Vue.js app (
App.vue), import theRepositoriescomponent and use it in the template:
- In your main Vue.js app (
<template>
<div>
<Repositories />
</div>
</template>
<script>
import Repositories from "./components/Repositories.vue";
export default {
components: { Repositories, },
};
</script>
This code displays the first 10 repositories of the user "octocat" and a "Load more" button that fetches the next page of repositories when clicked
The
axioslibrary is used to make an HTTP GET request to the GitHub API with the appropriate query parameters and headersThe fetched repository data is stored in the
reposarray in the component's data objectThe
loadMoremethod updates thepageparameter and fetches the next page of repositories
Displaying repository details on a separate page
Once we have fetched the repository data from the API, we can create a new Vue.js component to display the repository details on a separate page
We can use Vue Router to create a new route for the repository details page
We can pass the repository data as props to the repository details component
For example, the following code creates a new
RepoDetailscomponent that displays the details of a single
Style the component
You can add styles to the
Repositoriescomponent by adding a<style>section to the component fileFor example, the following CSS styles the repository cards and the "Load more" button:
