Skip to content

Instantly share code, notes, and snippets.

@handrihmw
Last active August 10, 2023 02:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save handrihmw/0b9dbcd086bbdd1b347057e6149d2a06 to your computer and use it in GitHub Desktop.
Save handrihmw/0b9dbcd086bbdd1b347057e6149d2a06 to your computer and use it in GitHub Desktop.
Get API with Vue JS
// Optional API
<script>
export default {
data() {
return {
listItems: []
}
},
methods: {
async getData() {
const res = await fetch("https://jsonplaceholder.typicode.com/posts");
const finalRes = await res.json();
this.listItems = finalRes;
}
},
mounted() {
this.getData()
}
}
</script>
// Composition API
<script setup>
import { ref } from 'vue';
const listItems = ref([]);
async function getData() {
const res = await fetch("https://jsonplaceholder.typicode.com/posts");
const finalRes = await res.json();
listItems.value = finalRes;
}
getData()
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment