๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
[๊ฐœ๋ฐœ] Practice/Vue.js

[Vue.js] ์„œ๋ฒ„๋กœ ajax ์š”์ฒญํ•˜๋Š” ๋”๋ณด๊ธฐ ๋ฒ„ํŠผ ๋งŒ๋“ค๊ธฐ

by Connecting-the-dots 2022. 4. 6.
728x90
๋ฐ˜์‘ํ˜•

๐Ÿ’ก ์‹ค์Šต ํฌ์ธํŠธ!


๐Ÿ’œ Ajax ์•Œ์•„๋ณด๊ธฐ


๐Ÿ’œ axios ๋ฅผ ์ด์šฉํ•˜์—ฌ GET ์š”์ฒญํ•˜๊ธฐ

import axios from 'axios';

axios.get('URL').then( result => {
  // GET ์š”์ฒญ ์„ฑ๊ณตํ•  ๊ฒฝ์šฐ ์‹คํ–‰ํ•  ์ฝ”๋“œ ์ž‘์„ฑํ•˜๊ธฐ
  console.log(result);
}).catch(()=>{
  // GET ์š”์ฒญ ์‹คํŒจํ•  ๊ฒฝ์šฐ ์‹คํ–‰ํ•  ์ฝ”๋“œ ์ž‘์„ฑํ•˜๊ธฐ
})
  • ์œ„์™€ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋ฉด ์›ํ•˜๋Š” ์„œ๋ฒ„์˜ URL ๋กœ GET ์š”์ฒญ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • .then() ์•ˆ์—๋Š” function(){ } ๊ณผ ๊ฐ™์ด ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•ด์ค€ ๋‹ค์Œ, GET ์š”์ฒญ์ด ์„ฑ๊ณตํ•  ๊ฒฝ์šฐ์— ์‹คํ–‰ํ•  ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์ฃผ๋ฉด ๋œ๋‹ค.
  • GET ์š”์ฒญ์„ ํ†ตํ•ด ๊ฐ€์ ธ์˜จ ๋ฐ์ดํ„ฐ๋Š” result ๋ผ๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ์— ๋‹ด๊ฒจ์žˆ์œผ๋ฏ€๋กœ, ๊ถ๊ธˆํ•˜๋‹ค๋ฉด console.log()๋กœ ์ถœ๋ ฅํ•ด๋ณด๋ฉด ๋œ๋‹ค.
  • ajax ์š”์ฒญ์ด ์‹คํŒจํ•  ๊ฒฝ์šฐ์— ํŠน์ • ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด .catch() ์•ˆ์— ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•ด์ค€ ๋‹ค์Œ ์‹คํ–‰ํ•  ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์ฃผ๋ฉด ๋œ๋‹ค.
  • ๋ณดํ†ต ajax ์š”์ฒญ์ด ์‹คํŒจํ•˜๋Š” ๊ฒฝ์šฐ๋Š” URL ์„ ์ž˜๋ชป ์“ฐ๊ฑฐ๋‚˜, ์„œ๋ฒ„๊ฐ€ ๋‹ค์šด๋˜๋Š” ๊ฒฝ์šฐ ๋“ฑ์ด ์žˆ๋‹ค.

๐Ÿ’œ axios ๋ฅผ ์ด์šฉํ•˜์—ฌ POST ์š”์ฒญํ•˜๊ธฐ

import axios from 'axios';

axios.post('URL', '๋ณด๋‚ผ_๋ฐ์ดํ„ฐ').then( result => {
  // POST ์š”์ฒญ ์„ฑ๊ณตํ•  ๊ฒฝ์šฐ ์‹คํ–‰ํ•  ์ฝ”๋“œ ์ž‘์„ฑํ•˜๊ธฐ
  console.log(result);
}).catch(()=>{
  // POST ์š”์ฒญ ์‹คํŒจํ•  ๊ฒฝ์šฐ ์‹คํ–‰ํ•  ์ฝ”๋“œ ์ž‘์„ฑํ•˜๊ธฐ
})
  • ajax ์— POST ์š”์ฒญ๋„ ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, POST ์š”์ฒญ์ด๋ž€ ์„œ๋ฒ„๋กœ ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•˜๋Š” ๊ฒƒ์ด๋‹ค.
  • ์ด ๋•Œ, ์ „์†กํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋กœ๋Š” ๋ฌธ์ž์—ด์ด๋“  object ์ž๋ฃŒํ˜•์ด๋“  ๋ชจ๋‘ ๊ฐ€๋Šฅํ•˜๋‹ค.

๐Ÿ’œ "๋”๋ณด๊ธฐ" ๋ฒ„ํŠผ์œผ๋กœ ๊ฐ€์ ธ์˜จ ๋ฐ์ดํ„ฐ๋ฅผ Post.vue ๋กœ ๋ณด์—ฌ์ฃผ๊ธฐ

// ์ „์ฒด ์ฝ”๋“œ ๋ฏธ๋ฆฌ๋ณด๊ธฐ
// ์ „์ฒด ์ฝ”๋“œ์˜ ๋‚ด์šฉ์ด ๊ธธ์–ด css ๋ถ€๋ถ„์€ ์ œ์™ธํ–ˆ๋‹ค.

<template>
  <div class="header">
    <ul class="header-button-left">
      <li>Cancel</li>
    </ul>
    <ul class="header-button-right">
      <li>Next</li>
    </ul>
    <img src="./assets/logo.png" class="logo" />
  </div>

  <Container :posts="posts" />

  <button class="btn" @click="more">๋”๋ณด๊ธฐ</button>
  <div class="footer">
    <ul class="footer-button-plus">
      <input type="file" id="file" class="inputfile" />
      <label for="file" class="input-plus">+</label>
    </ul>
  </div>
</template>

<script>
import Container from "./components/Container.vue";
import posts from "./assets/posts";
import axios from "axios";
export default {
  name: "App",
  data() {
    return {
      posts: posts,
      moreCnt: 0,
    };
  },
  methods: {
    more() {
      axios
        .get(`https://codingapple1.github.io/vue/more${this.moreCnt}.json`)
        .then((result) => {
          this.posts.push(result.data);
          this.moreCnt++;
        })
        .catch(() => {
          console.log("GET ์š”์ฒญ์„ ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.");
        });
    },
  },
  components: {
    Container: Container,
  },
};
</script>

<style>
</style>
  • "๋”๋ณด๊ธฐ" ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ๊ฐ•์‚ฌ๋‹˜์ด ์ค€๋น„ํ•ด์˜ค์‹  URL ๋กœ GET ์š”์ฒญ์„ ํ•œ ํ›„, Post.vue ๋กœ ๋ณด์—ฌ์ฃผ์–ด์•ผ ํ•œ๋‹ค.
  • ๋จผ์ €, "๋”๋ณด๊ธฐ" ๋ฒ„ํŠผ์„ ๋งŒ๋“  ๋‹ค์Œ, ํ•ด๋‹น ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด more ์ด๋ผ๋Š” method ๊ฐ€ ์‹คํ–‰๋˜๋„๋ก ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์—ˆ๋‹ค.
  • ๊ทธ๋Ÿฌ๊ณ  ๋‚˜์„œ, methods ์— more() ํ•จ์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ์—ˆ๋Š”๋ฐ ์ฝ”๋“œ๋ฅผ ํ•ด์„ํ•˜์ž๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
    • https://codingapple1.github.io/vue/more0.json ์— GET ์š”์ฒญ์„ ํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์˜จ๋‹ค.
    • ๋ฐ›์•„์˜จ ๋ฐ์ดํ„ฐ๋ฅผ result ์— ์ €์žฅํ•œ ๋‹ค์Œ ๋‚ด๊ฐ€ ํ•„์š”๋กœ ํ•˜๋Š” ๊ฐ์ฒด ํ˜•์ฒด์˜ ๋ฐ์ดํ„ฐ์ธ result.data ๋ฅผ ํ˜„์žฌ data ๋ณด๊ด€ํ•จ์˜ posts ์— push ํ•œ๋‹ค.
      (ํ˜„์žฌ posts ๋ผ๋Š” ๋ฐ์ดํ„ฐ์˜ ์•ž๋ถ€๋ถ„์— ์ถ”๊ฐ€ํ•  ๊ฒฝ์šฐ unshift(), ๋’ท๋ถ€๋ถ„์— ์ถ”๊ฐ€ํ•  ๊ฒฝ์šฐ push() ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.)
  • ์œ„ ์ฝ”๋“œ๋Š” "๋”๋ณด๊ธฐ" ๋ฒ„ํŠผ์„ ํ†ตํ•ด GET ์š”์ฒญ์„ ํ•  URL ์ด ํ•˜๋‚˜์ธ ๊ฒฝ์šฐ์ด๊ณ , ๋ณดํ†ต์€ "๋”๋ณด๊ธฐ" ๋ฒ„ํŠผ์„ ๊ณ„์†ํ•ด์„œ ๋ˆ„๋ฅด๋ฉด ์„œ๋ฒ„์—์„œ ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ๋ฅผ ๋” ๋ถˆ๋Ÿฌ์™€์•ผํ•œ๋‹ค.
  • ๊ฐ•์‚ฌ๋‹˜์˜ ๊ฒฝ์šฐ ๋‘๋ฒˆ์งธ ์„œ๋ฒ„URL ์„ https://codingapple1.github.io/vue/more1.json ๋กœ ์•Œ๋ ค์ฃผ์…จ๋Š”๋ฐ, ์ฒซ๋ฒˆ์งธ ์„œ๋ฒ„URL ๊ณผ ๋น„๊ตํ–ˆ์„ ๋•Œ more ๋’ค์— ์ˆซ์ž๋งŒ ์ฆ๊ฐ€ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.
  • ๋”ฐ๋ผ์„œ, "๋”๋ณด๊ธฐ" ๋ฒ„ํŠผ์„ ํด๋ฆญํ•  ๋•Œ๋งˆ๋‹ค 0์—์„œ๋ถ€ํ„ฐ ์ˆซ์ž๊ฐ€ 1์”ฉ ์ฆ๊ฐ€ํ•˜๋„๋ก ํ•œ ๋‹ค์Œ, ํ•ด๋‹น ์ˆซ์ž๋ฅผ ์„œ๋ฒ„URL ์— ๋ฐ์ดํ„ฐ๋ฐ”์ธ๋”ฉ์„ ํ•ด์คŒ์œผ๋กœ์„œ, ๋งค๋ฒˆ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค.

 

728x90
๋ฐ˜์‘ํ˜•