Skip to content

Instantly share code, notes, and snippets.

@sharmadhiraj
Created January 2, 2019 18:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sharmadhiraj/b5de4f4dc3d0624568815f1c6f40a970 to your computer and use it in GitHub Desktop.
Save sharmadhiraj/b5de4f4dc3d0624568815f1c6f40a970 to your computer and use it in GitHub Desktop.
Vue.js | Making API calls using Axios
<!DOCTYPE html>
<html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Vue.js | Making API calls using Axios</title>
</head>
<body style="text-align: center">
<div id="app" style="display: inline-block;margin-top: 100px">
<img v-bind:src="avatar" alt="">
<h1 style="margin-bottom: 0">{{name}}</h1>
<span>{{email}}</span>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.2/vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
const data = {avatar: "https://ui-avatars.com/api/?size=128", name: "john doe", email: "johndoe@email.com"};
var vm = new Vue({
el: '#app',
data: data,
mounted() {
axios.get('https://randomuser.me/api/')
.then(function (response) {
const user = response.data.results[0];
vm.avatar = user.picture.large;
vm.name = user.name.first + " " + user.name.last;
vm.email = user.email;
})
}
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment