IT/development

[Vue.js] ์‹ฑ๊ธ€ ํŒŒ์ผ ์ปดํฌ๋„ŒํŠธ์—์„œ axios ํ†ต์‹ 

์•Œ ์ˆ˜ ์—†๋Š” ์‚ฌ์šฉ์ž 2023. 7. 29.

post ๋ฐฉ์‹์œผ๋กœ ๋ฐ์ดํ„ฐ ์ „์†ก

#ajax ํ†ต์‹  ์œ„ํ•œ lib ์„ค์น˜
npm i axios

App.vue

<template>
	<!-- v-on:submit.prevent(๊ธฐ๋ณธ๋™์ž‘ ๋ง‰์Œ) -->
  <form v-on:submit.prevent="submitForm">
    <div>
      <label for="username">id: </label>
			<!-- v-model์„ username๊ณผ ์—ฐ๊ฒฐ(๋ฐ์ดํ„ฐ ๋ณ€๊ฒฝ ์‹œ ์ธ์Šคํ„ด์Šค์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅ) -->
      <input v-model="username" id="username" type="text">
    </div>
    <div>
      <label for="password">password: </label>
			<!-- v-model์„ password์™€ ์—ฐ๊ฒฐ -->
      <input v-model="password" id="password" type="password">
    </div>
		<!-- ํด๋ฆญ์ด๋ฒคํŠธ๋กœ save ์ •์˜ -->
    <button @click="save" type="submit">login</button>
  </form>       
</template>

<script>
//axios import
import axios from 'axios';
  
export default {
  data: function() {
    return {
			//v-model๋กœ ์—ฐ๊ฒฐํ•œ ๋ฐ์ดํ„ฐ
      username: '',
      password: ''
    }
  },   
  methods: {  
		//submit ์‹œ ์‹คํ–‰๋˜๋Š” ํ•จ์ˆ˜
    submitForm: function() {
        // e.preventDefault(); -> v-on:submit.prevent๊ฐ€ ์ด ์—ญํ• 
        console.log(this.username + "\n" + this.password);  
				//post๋ฐฉ์‹์œผ๋กœ ๋ฐ์ดํ„ฐ ์ „์†กํ•  ์„œ๋ฒ„
        const url = 'https://jsonplaceholder.typicode.com/users';
				//์„œ๋ฒ„๋กœ ์ „์†กํ•  ๋ฐ์ดํ„ฐ(์ž…๋ ฅ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ด๊ณณ์— ๋ฐ”์ธ๋”ฉ)
        const data = {
					// document.getElementById('username') or $("#username").val() ์‚ฌ์šฉ ํ•„์š” X
          username: this.username,
          password: this.password
        }
				//ajax ํ†ต์‹ 
        axios.post(url, data)   
					//์„ฑ๊ณต
          .then(function(response){
            console.log(response);
          })  
					//์‹คํŒจ
          .catch(function(error){
            console.log(error);
          });
    }
  }
}
</script>

<style>

</style>


get๋ฐฉ์‹์œผ๋กœ ๋ฐ์ดํ„ฐ ์กฐํšŒ

<template>
  <div> 
    <app-header></app-header>
    <button type="button" @click="getList">์กฐํšŒ</button>
    <app-footer></app-footer> 
  </div>
</template>

<script>

import AppHeader from './components/AppHeader';
import AppFooter from './components/AppFooter';
import axios from 'axios';

export default {
  components: {
    'app-header': AppHeader,
    'app-footer': AppFooter,
  },    
  methods: {
    getList: function() {   
      const url = 'http://localhost:9090/api/board'; 
      axios.get(url)  
      .then(function(response) {  
        console.log(response);          
      })
      .catch(function(error) {  
        console.log(error);           
      }); 
    }
  }
}
</script>

<style>
</style>

queryString(?empId=dev&empNm=๋กœ๋‹ค์ฃผ&empEmail=dev@naver.com) ์ถ”๊ฐ€ ์‹œ ์•„๋ž˜์ฒ˜๋Ÿผ ์‚ฌ์šฉ

const data = {
  empId: this.empId,
  empNm: this.empNm,
  empEmail: this.empEmail          
}

//2๋ฒˆ ์งธ ์ธ์ž๊ฐ’์— ์œ„์—์„œ ๋งŒ๋“  object ์ถ”๊ฐ€
axios.get(url, { data })

reference: https://www.inflearn.com/course/lecture?courseSlug=age-of-vuejs&unitId=21471&tab=curriculum 

 

ํ•™์Šต ํŽ˜์ด์ง€

 

www.inflearn.com

 

๋Œ“๊ธ€