๐ค ์์งlog ๐ค
[VUE] ๋ฐ์ดํฐ ๋ฐ์ธ๋ฉ & v-for ๋ฐ๋ณต๋ฌธ & ์ด๋ฒคํธ ํธ๋ค๋ฌ ๋ณธ๋ฌธ
[VUE] ๋ฐ์ดํฐ ๋ฐ์ธ๋ฉ & v-for ๋ฐ๋ณต๋ฌธ & ์ด๋ฒคํธ ํธ๋ค๋ฌ
Eun_zii 2023. 10. 20. 15:15React ์ํด์ด์์ง๋ง ๋ฌ๋์ปค๋ธ๊ฐ ๋ฎ๊ณ ์ฝ๋ค๋ ์๊ธฐ์ 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( ) ์คํํ๋ฏ์ด ํด์ฃผ๋ฉด ๋๋ค ! !
โค๏ธ ๋์์ฃผ์ ๋ถ :
'๐ Vue' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[VUE] Vue๋ก ๋ง๋ ๊ฒ์ํ์ CRUD ๊ตฌํํ๊ธฐ (1) | 2023.10.27 |
---|