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 ์™„๋ฒฝ ๋งˆ์Šคํ„ฐ: ๊ธฐ์ดˆ๋ถ€ํ„ฐ ์‹ค์ „๊นŒ์ง€ - โ€œ๊ธฐ๋ณธํŽธโ€

์†Œ์Šค์ฝ”๋“œ ๋ฐ”๋กœํ™•์ธํ•˜๊ธฐ

Frontend

๋Œ“๊ธ€ ์“ฐ๊ธฐ