Skip to content

Instantly share code, notes, and snippets.

@fayazara
Created November 13, 2019 07:18
Show Gist options
  • Save fayazara/a1e5e319f56a108c2eb2690ae8a9e469 to your computer and use it in GitHub Desktop.
Save fayazara/a1e5e319f56a108c2eb2690ae8a9e469 to your computer and use it in GitHub Desktop.
Vuejs Async/Await Vuejs Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.8.0/css/bulma.min.css">
</head>
<body>
<div id="app" class="section">
<button class="button is-dark" :class="{'is-loading': loading}" @click="getposts">Get posts</button>
<br>
<ul v-for="post in posts">
<li>
<div class="card">
<div class="card-content">
<div class="content">
<p>{{post.title}}</p>
</div>
</div>
</div>
</li>
</ul>
</div>
<script>
new Vue({
el: "#app",
data: {
posts: "",
loading: false
},
methods: {
async getposts() {
this.loading = true;
try {
const response = await axios.get(`http://slowwly.robertomurray.co.uk/delay/4000/url/https://jsonplaceholder.typicode.com/todos/`)
this.posts = response.data;
this.loading = false;
} catch (e) {
this.loading = false;
alert(e);
}
}
}
})
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment