๐ Language/Vue
[Age of Vue] Vue.js ์์ํ๊ธฐ - 22. ์ก์์ค์ค(Axios)
a n u e
2022. 5. 7. 21:13
Axios?
Vue์์ ๊ถ๊ณ ํ๋ HTTP ํต์ ์ ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
- promise ๊ธฐ๋ฐ. API๊ฐ ๋ค์ํ๋ค.
(์ฐธ๊ณ - ์๋ฐ์คํฌ๋ฆฝํธ ๋น๋๊ธฐ ์ฒ๋ฆฌ ํจํด callback > promise > promise + generator > async & await)
์คํ API๋ฅผ ์ด์ฉํ axios ํ ์คํธ
<!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>Axios</title>
</head>
<body>
<div id="app">
<button v-on:click="getData">get user</button>
<div>
{{ users }}
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
new Vue({
el: '#app',
data: {
users: []
},
methods: {
getData: function() {
var vm = this;
axios.get('https://jsonplaceholder.typicode.com/users/')
.then(function(response) {
console.log(response.data);
vm.users = response.data;
})
.catch(function(error) {
console.log(error);
});
}
}
})
</script>
</body>
</html>
์ฐธ๊ณ