Last active
August 10, 2023 02:58
-
-
Save handrihmw/0b9dbcd086bbdd1b347057e6149d2a06 to your computer and use it in GitHub Desktop.
Get API with Vue JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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