props
props
props
๋ ๋ถ๋ชจcomponent๊ฐ ์์component์๊ฒ data๋ฅผ ์ ๋ฌํ๋ ๊ฒ
์์ component
์์component๋ฅผ AppCard.vue
๋ก ์ ์
html
<template>
<div>
<p>
{{ $props }} <!--$๋ก๋ ๋งํฌ์
์ผ๋ก ํํ๊ฐ๋ฅ-->
</p>
<span>{{ type == 'news' ? '๋ด์ค' : '๊ณต์ง์ฌํญ'}}</span> <!--computed๋ก ํํ๊ฐ๋ฅ-->
<h2>
{{ title }}
</h2>
<p>
{{ contents }}
</p>
<p :class="isLikeClass"> <!--computed ์ฌ์ฉ-->
์ข์์
</p>
</div>
</template>
js
<script setup>
const props = defineProps({
type: {
type: String,
default: 'news',
validator: value => {
return 'undefined' || ['news', 'notice'].includes[value];
}, //์ ์ธ์ด ์๋์ด๋ ๊ฐ๋ฅํ์ง๋ง ์ ์ธํ ๊ฒฝ์ฐ news๋ notice๋ง ๊ฐ๋ฅํ๋ค
},
title: String,
contents: String,
isLike: {
type: Boolean
}
});
const isLikeClass = computed(() => props.isLike ? 'btn-good': 'btn-bad');
</script>
css
div{
margin-bottom: 40px;
border: 10px solid;
}
.btn-good{
background: red;
}
.btn-bad{
background: green;
}
๋ถ๋ชจ component
๋ถ๋ชจcomponent๋ฅผ TheView.vue
๋ก ์ ์
html
<!-- ์ง์ ๊ฐ๋ฃ๊ธฐ -->
<AppCard title="์ ๋ชฉ1" contents="๋ด์ฉ1"></AppCard>
<!-- ๊ฐ์ฒด์ ํ๋๊ฐ ์ฌ์ฉ -->
<AppCard :title="post1.title" :contents="post1.contents" :isLike="post1.isLike"></AppCard>
<!-- ๊ฐ์ฒด ์ฌ์ฉ -->
<AppCard v-bind="post3"></AppCard>
<!-- ๊ฐ์ฒด+๋ฐ๋ณต๋ฌธ -->
<div v-for = "post in posts" :key="post.id">
<AppCard
:title="post.title"
:contents="post.contents"
:type="post.type"
:isLike="post.isLike"
></AppCard>
</div>
js
const post1 = {
title: '์ ๋ชฉ2',
contents: '๋ด์ฉ2',
isLike: true,
}
const post3 = {
title: '์ ๋ชฉ3',
contents: '๋ด์ฉ3',
isLike: true,
type: "notype",
}
const posts = reactive([
{title:'์ ๋ชฉ4', contents:'๋ด์ฉ4', isLike:false, type:'notice'},
{title:'์ ๋ชฉ5', contents:'๋ด์ฉ5', isLike:false, type:'notice'},
]);
์ฐธ๊ณ ์๋ฃ: Vue3 ์๋ฒฝ ๋ง์คํฐ: ๊ธฐ์ด๋ถํฐ ์ค์ ๊น์ง - โ๊ธฐ๋ณธํธโ
์์ค์ฝ๋ ๋ฐ๋กํ์ธํ๊ธฐ
๋๊ธ ์ฐ๊ธฐ