Created
April 7, 2021 18:44
-
-
Save cytrinox/88e7f134c26d9b1b1dd25f88c6e46249 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<template> | |
<h1>Vue 3 and Fetch Example</h1> | |
<ul v-if="!loading && posts && posts.length"> | |
<li>foooo</li> | |
<li v-for="post of posts" v-bind:key="post.id"> | |
<p><strong>{{ post.title }}</strong></p> | |
<p>{{ post.body }}</p> | |
</li> | |
</ul> | |
<p v-if="loading">Still loading..</p> | |
<p v-if="error">This is error</p> | |
</template> | |
<script lang="ts"> | |
import { defineComponent } from 'vue'; | |
//import { onMounted, onUpdated, onUnmounted } from 'vue' | |
export default defineComponent({ | |
name: 'Posts', | |
methods: { | |
fetchData() { | |
fetch('http://jsonplaceholder.typicode.com/posts' | |
, { | |
method: "get", | |
headers: { | |
"content-type": "application/json", | |
}} | |
).then(response => response.json()) | |
.then((data) => this.$data.posts = data); | |
}, | |
}, | |
data() { | |
return { | |
posts: [], | |
loading: false, | |
error: null, | |
}; | |
}, | |
mounted() { | |
this.fetchData(); | |
}, | |
}); | |
</script> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment