Axios 간단한 설명과 API 통신 결과를 화면으로 보내는 기초 예제
*자세한 설명 생략
▷ Axios 란?
- Vue에서 권고하는 Promise 기반의 HTTP 라이브러리
*Promise : 비동기 처리
- 서버와 HTTP 통신
- Ajax, fetch와 같은 웹 통신 기능
- JSON 데이터 자동 변환
▷ Axios 설치
*vue-cli를 통해 Project 생성 후 설치
💡npm install axios
▷ Axios 사용 방법(Post)
axios
.post(URI, data)
.then((res) => { // 성공
...
})
.catch((res) => { // 실패
...
});
▷ 기초 예제 - HTTP 통신
- main.js
*provide / inject를 사용하여 전역 변수 사용
*provide / inject 설명 생략
*Axios 모듈 사용 등록
import { createApp } from 'vue'
import App from './App.vue'
import { router } from './router/MenuNav.js'
import Axios from "axios";
createApp(App)
/** 모듈 사용 등록 */
.use(router)
/** 전역 설정 */
// Axios
.provide('$Axios', Axios)
// API URI
.provide('$BHS_CMM_LGN', "http://IP:Port/bhs/cmm/login.do")
/** 연결 */
.mount('#app');
- APICallTestCmp.vue
<template>
<div class="APICallTestCmp">
<h1>👨🏫 API Call Test </h1><br /><br />
<h4>아이디 : <input v-model="id" placeholder="아이디"></h4>
<h4>패스워드 : <input v-model="pwd" placeholder="패스워드"></h4>
<button @click="call(id, pwd);">API Call</button>
</div>
<div>
<h4>API Response</h4>
<div style="width: 700px; height: 300px; overflow: auto;">
{{ result }}
</div>
</div>
</template>
<script setup>
import { inject, ref } from 'vue';
const URI = inject("$BHS_CMM_LGN");
const Axios = inject("$Axios");
const result = ref("");
function call(id, pwd) {
const data =
{
"id": id,
"pwd": pwd
};
Axios
.post(URI, data)
.then((res) => {
const resVO = res.data;
if (resVO.resultCode == "0000") {
result.value = resVO.jwt;
} else {
alert(resVO.resultMsg);
}
})
.catch(() => {
console.log("실패");
})
}
</script>
▷결과 화면
▷참고
Vue.js에서 Axios를 이용한 비동기 통신
Vue.js에서 Axios를 이용한 비동기 통신 Vue에서 권고하는 Promise 기반의 HTTP 라이브러리로서 웹 어플리케이션에서 서버와 HTTP 요청을 주고 받을 수 있다. 또 Promise를 사용하면 비동기식 처리 상에서
jiurinie.tistory.com
시작하기 | Axios Docs
시작하기 브라우저와 node.js에서 사용할 수 있는 Promise 기반 HTTP 클라이언트 라이브러리 Axios란? Axios는 node.js와 브라우저를 위한 Promise 기반 HTTP 클라이언트 입니다. 그것은 동형 입니다(동일한 코
axios-http.com
[Vue] Axios를 이용한 http 통신 - 설치부터 예제, 비동기 처리까지.
목차 1. Axios 란? 2. Axios 설치 3. Axios 문법 4. Axios 사용 방법 (post) 5. Vue 에서 Axios 설정 6. Axios 예제 (Get, Post) 7. Axios 비동기처리 예제 (Get, Post) https://axios-http.com/kr/docs/intro 시작하기 | Axios Docs 시작하
icea.tistory.com
▷ 관련 글
Vue3 기초 예제 프로젝트 정리
Vue3 기초 예제 프로젝트 구조 및 소스 중간 정리 *관련 글이 점점 늘어나 하나로 통합하기 위함 *이 글에서 만 싱크 맞춤 *주석 이외 설명 생략 *자세한 설명 생략 ▷ 프로젝트 전체 구조 < 파일 따
coding-today.tistory.com
'▶ Front-End > Vue.js' 카테고리의 다른 글
Vue3 기초 예제 - Axios 모듈화(async/await) (0) | 2023.10.30 |
---|---|
Vue3 기초 예제 - 로그인(pinia) (2) | 2023.10.30 |
Vue3 기초 예제 - Router(메뉴 네비게이션) (0) | 2023.10.26 |
Vue3 기초 예제 - Dynamic Select Box (0) | 2023.10.25 |
Vue 기초 예제 - Directive(조건/반복) (0) | 2023.10.23 |
댓글