๐Ÿค ์€์ง€log ๐Ÿค

[VUE] ๋ฐ์ดํ„ฐ ๋ฐ”์ธ๋”ฉ & v-for ๋ฐ˜๋ณต๋ฌธ & ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ๋ณธ๋ฌธ

๐Ÿ“ Vue

[VUE] ๋ฐ์ดํ„ฐ ๋ฐ”์ธ๋”ฉ & v-for ๋ฐ˜๋ณต๋ฌธ & ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ

Eun_zii 2023. 10. 20. 15:15

React ์›ํˆด์ด์˜€์ง€๋งŒ ๋Ÿฌ๋‹์ปค๋ธŒ๊ฐ€ ๋‚ฎ๊ณ  ์‰ฝ๋‹ค๋Š” ์–˜๊ธฐ์— vue ๊ณต๋ถ€๋ฅผ ์‹œ์ž‘ํ•ด ๋ดค๋Š”๋ฐ 

๋œจ๋“ ๐Ÿ˜น ๋„ˆ๋ฌด ์‰ฝ์ž๋‚˜,, ๊ฐ„๋‹จํ•ด ,, ์ด๊ฒŒ ๋ญ๋žŒ ,, ์™œ ์ด์ œ ์•ˆ ๊ฑฐ์ง€ ,, 

 

๋ฌผ๋ก  ๋ฆฌ์•กํŠธ๋งŒํผ ๊นŠ๊ฒŒ ํŒŒ๊ณ ๋“ ๊ฑด ์•„๋‹ˆ์ง€๋งŒ ๊ณต๋ถ€ํ•˜๋Š” ๋‚ด๋‚ด ์žฌ๋ฏธ์žˆ์—ˆ๋‹ค ใ…Ž_ใ…Ž!

 

์ฝ”๋”ฉ์• ํ”Œ๋‹˜ ๊ฐ•์˜ ์ค‘ ๋ฌด๋ฃŒ๊ฐ•์˜ ๋ถ€๋ถ„๋งŒ ๋ณด๊ณ  ์กฐ๊ธˆ ๊ณต๋ถ€ํ•ด ๋ดค๋‹ค.

 

 

 

๐Ÿ”† ๋ฐ์ดํ„ฐ ๋ฐ”์ธ๋”ฉ { {  } }

:  1. script ํƒœ๊ทธ ๋‚ด์— data( ){ return { } } ๋ฐ์ดํ„ฐ์ €์žฅ์†Œ๋ฅผ ๋งŒ๋“ค์–ด ๋‘๊ณ  object์ž๋ฃŒ ํ˜•์‹์œผ๋กœ ์ž‘์„ฑ ํ›„ { { ๋ฐ์ดํ„ฐ์ด๋ฆ„ } }
   2. HTMLํƒœ๊ทธ ์†์„ฑ๋„ ๋ฐ์ดํ„ฐ๋ฐ”์ธ๋”ฉ๊ฐ€๋Šฅ -> ์†์„ฑ ์•ž์— : ๋ถ™์—ฌ์ฃผ๋ฉด ๋จ ex) :์†์„ฑ="๋ฐ์ดํ„ฐ์ด๋ฆ„"

 

<script>
export default {
  name: "App",
  data() {
    return {
      modal: false,
      menuName: ["HOME", "SHOP", "CONTACT"],
      product: ["์—ญ์‚ผ๋™ ์›๋ฃธ","๋งˆํฌ๊ตฌ ์›๋ฃธ","์ฒœํ˜ธ๋™ ์›๋ฃธ"], ๐Ÿ“Œ
    };
  },
  methods: {},
  components: {},
};
</script>

<template>
  <div class="product">
    <h3>{{ products[0] }}</h3> ๐Ÿ“Œ
    <h4>70๋งŒ์›</h4>
  </div>
  
  <div class="product">
    <h3>{{ products[1] }}</h3>
    <h4>50๋งŒ์›</h4>
  </div>
  
    <div class="product">
    <h3>{{ products[2] }}</h3>
    <h4>100๋งŒ์›</h4>
  </div>
  
</template>

 

๐Ÿ”† v-for ๋ฐ˜๋ณต๋ฌธ

: 1. <ํƒœ๊ทธ v-for=" name in ๋ฐ˜๋ณต ํšŸ์ˆ˜" :key="name">

  2. <ํƒœ๊ทธ v-for=" name in array/object " :key="name">

 

<script>
export default {
  name: "App",
  data() {
    return {
      modal: false,
      menuName: ["HOME", "SHOP", "CONTACT"], ๐Ÿ“Œ
    };
  },
  methods: {},
  components: {},
};
</script>

<template>

  <div class="menu">
    <a v-for="x in menuName" :key="x">{{ x }}</a> ๐Ÿ“Œ
  </div>
  
</template>

 

๐Ÿ”† ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ

: v-on:click="" / @click=""

 

<script>
export default {
  name: "App",
  data() {
    return {
      modal: false, ๐Ÿ“Œ
      menuName: ["HOME", "SHOP", "CONTACT"],
      products: posts,
    };
  },
  methods: {},
  components: {},
};
</script>

<template>
  <div class="black-bg" v-if="modal === true">
    <div class="white-bg">
      <h3>์ƒ์„ธํŽ˜์ด์ง€</h3>
      <h4>์ƒ์„ธํŽ˜์ด์ง€ ๋‚ด์šฉ ! ! !</h4>
      <button @click="modal = false">๋‹ซ๊ธฐ</button> ๐Ÿ“Œ
    </div>
  </div>

  <div v-for="(a, i) in posts" :key="i">
    <img :src="posts[i].image" class="image-size" @click="modal = true" /> ๐Ÿ“Œ
    <h3>{{ a.title }}</h3>
    <h4>{{ a.content }}</h4>
    <h4>{{ a.price }}์›</h4>
  </div>
  
</template>

 

๐Ÿ”† Vue์—์„œ ๋™์  UI ๋งŒ๋“ค ๋•Œ 

: 1. UI ๋””์ž์ธ ๋งŒ๋“ค์–ด ๋‘๊ธฐ (ex. Modal ๋“ฑ๋“ฑ )

  2. UI์˜ ํ˜„์žฌ ์ƒํƒœ๋ฅผ ๋ฐ์ดํ„ฐ๋กœ ๋งŒ๋“ค๊ธฐ (ex. ๋ชจ๋‹ฌ์ด ํ˜„์žฌ ์—ด๋ ค์žˆ๋Š”์ง€ ์•„๋‹Œ์ง€ ๋“ฑ๋“ฑ)

  3. ๋ฐ์ดํ„ฐ์— ๋”ฐ๋ผ UI๊ฐ€ ์–ด๋–ป๊ฒŒ ๋ณด์ผ์ง€ ์ž‘์„ฑ (ex. v-if="์กฐ๊ฑด๋ฌธ" ๋“ฑ๋“ฑ)

 

<script>
export default {
  name: "App",
  data() {
    return {
      modal: false, ๐Ÿ“Œ
    };
  },
  methods: {},
  components: {},
};
</script>

<template>
  <div class="black-bg" v-if="modal === true"> ๐Ÿ“Œ
    <div class="white-bg">
      <h3>์ƒ์„ธํŽ˜์ด์ง€</h3>
      <h4>์ƒ์„ธํŽ˜์ด์ง€ ๋‚ด์šฉ ! ! !</h4>
      <button @click="modal = false">๋‹ซ๊ธฐ</button>
    </div>
  </div>
</template>

 

๐Ÿ”† Vue์—์„œ ํ•จ์ˆ˜ ๋งŒ๋“ค๋•Œ

: 1. <script></script> ๋‚ด์— methods:{ ํ•จ์ˆ˜๋ช…(){} }

  2. ํ•จ์ˆ˜ ์•ˆ์—์„œ ๋ฐ์ดํ„ฐ ์‚ฌ์šฉํ•  ๋•Œ this.๋ฐ์ดํ„ฐ๋ช… 

 

<script>
export default {
  name: "App",
  data() {
    return {
      modal: false, 
    };
  },
  methods: {}, ๐Ÿ“Œ
  components: {},
};
</script>

 

๐Ÿ”† ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด GIF ๊ฐ™์€ ์ง๋ฐฉ.. ๋น„์Šค๋ฌด๋ฆฌํ•œ ๊ฑฐ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค!

 

<script setup>
import posts from "./datas/post";
</script>

<template>
  <div class="black-bg" v-if="modal === true">
    <div class="white-bg">
      <h3>์ƒ์„ธํŽ˜์ด์ง€</h3>
      <h4>์ƒ์„ธํŽ˜์ด์ง€ ๋‚ด์šฉ ! ! !</h4>
      <button @click="modal = false">๋‹ซ๊ธฐ</button>
    </div>
  </div>

  <div class="menu">
    <a v-for="x in menuName" :key="x">{{ x }}</a>
  </div>

  <div v-for="(a, i) in posts" :key="i">
    <img :src="posts[i].image" class="image-size" @click="modal = true" />
    <h3>{{ a.title }}</h3>
    <h4>{{ a.content }}</h4>
    <h4>{{ a.price }}์›</h4>
  </div>
  
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      modal: false,
      menuName: ["HOME", "SHOP", "CONTACT"],
      products: posts,
    };
  },
  methods: {},
  components: {},
};
</script>

<style scoped>
body {
  margin: 0;
}

div {
  box-sizing: border-box;
}

.black-bg {
  width: 100%;
  height: 100%;
  background-color: rgb(0, 0, 0, 0.5);
  position: fixed;
  padding: 20px;
}
.white-bg {
  width: 100%;
  background-color: white;
  border-radius: 8px;
  padding: 20px;
}

.image-size {
  width: 100%;
  height: 30%;
}
.product {
  margin: 20px;
}

.menu {
  background-color: lightgray;
  padding: 10px;
  margin-bottom: 30px;
  border-radius: 5px;
  text-align: center;
}

.menu a {
  font-weight: bold;
  color: black;
  padding: 20px;
}

button {
  margin-right: 10px;
  background-color: red;
  color: white;
  border: none;
  border-radius: 5px;
  font-weight: bold;
}
</style>
// post.js

export default [
  {
    id: 0,
    title: "Sinrim station 30 meters away",
    image: "https://codingapple1.github.io/vue/room0.jpg",
    content: "18๋…„ ์‹ ์ถ•๊ณต์‚ฌํ•œ ๋‚จํ–ฅ ์›๋ฃธ โ˜€๏ธ, ๊ณต๊ธฐ์ฒญ์ •๊ธฐ ์ œ๊ณต",
    price: 340000,
  },
  {
    id: 1,
    title: "Changdong Aurora Bedroom(Queen-size)",
    image: "https://codingapple1.github.io/vue/room1.jpg",
    content: "์นจ์‹ค๋งŒ ๋”ฐ๋กœ ์žˆ๋Š” ๊ณต์šฉ ์…ฐ์–ดํ•˜์šฐ์Šค์ž…๋‹ˆ๋‹ค. ์ตœ๋Œ€ 2์ธ ๊ฐ€๋Šฅ",
    price: 450000,
  },
  {
    id: 2,
    title: "Geumsan Apartment Flat",
    image: "https://codingapple1.github.io/vue/room2.jpg",
    content: "๊ธˆ์‚ฐ์˜ค๊ฑฐ๋ฆฌ ์—ญ์„ธ๊ถŒ ์•„ํŒŒํŠธ์ž…๋‹ˆ๋‹ค. ์• ์™„๋™๋ฌผ ๋ถˆ๊ฐ€๋Šฅ ?",
    price: 780000,
  },
  {
    id: 3,
    title: "Double styled beds Studio Apt",
    image: "https://codingapple1.github.io/vue/room3.jpg",
    content: "๋ฌด์•”๋™์ธ๊ทผ 2์ธ์šฉ ์›๋ฃธ์ž…๋‹ˆ๋‹ค. ์ „์„ธ ์ „ํ™˜๊ฐ€๋Šฅ",
    price: 550000,
  },
  {
    id: 4,
    title: "MyeongIl Apartment flat",
    image: "https://codingapple1.github.io/vue/room4.jpg",
    content: "ํƒ„์ฒœ๋™ ์•„ํŒŒํŠธ ์›”์„ธ, ๋‚จํ–ฅ, ์—ญ 5๋ถ„๊ฑฐ๋ฆฌ, ํ—ˆ์œ„๋งค๋ฌผ์•„๋‹˜",
    price: 680000,
  },
  {
    id: 5,
    title: "Banziha One Room",
    image: "https://codingapple1.github.io/vue/room5.jpg",
    content: "๋ฐ˜์ง€ํ•˜ ์›๋ฃธ์ž…๋‹ˆ๋‹ค. ๋น„์˜ฌ ๋•Œ ๋ฌผ๊ฐ€๋” ์ƒˆ๋Š”๊ฑฐ ๋นผ๋ฉด ์ข‹์•„์š”",
    price: 370000,
  },
];

 

โœ… ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์™€์„œ ์ ์šฉํ•  ๋•Œ์—๋„ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ํ•˜๋ฉด ๋œ๋‹ค.

<script></script> ๋‚ด์— data( ) { } ์•ˆ์— ๋ฐ›์•„์˜จ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•ด ๋‘๊ณ , 

map( ) ์‹คํ–‰ํ•˜๋“ฏ์ด ํ•ด์ฃผ๋ฉด ๋œ๋‹ค ! !

 

 

โค๏ธ ๋„์›€์ฃผ์‹ ๋ถ„ :

 

 

728x90

'๐Ÿ“ Vue' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[VUE] Vue๋กœ ๋งŒ๋“  ๊ฒŒ์‹œํŒ์— CRUD ๊ตฌํ˜„ํ•˜๊ธฐ  (1) 2023.10.27