props

jude·2022년 2월 12일
0

vue

목록 보기
7/14
post-thumbnail

props

  • props 를 사용하여 부모 컴포넌트의 데이터를 자식 컴포넌트로 전달할 수 있다.
<!-- App.vue -->
<template>
  <Mybtn :bgColor="bgColor1" />
  <Mybtn large />
  <Mybtn :bgColor="bgColor2" :color="color1" />
</template>

<script>
import Mybtn from '~/components/MyBtn'

export default {
  components: {
    Mybtn
  },
  data() {
    return {
      bgColor1: 'royalblue',
      bgColor2: 'red',
      color1: 'black'
    }
  }
}
</script>
  • type과 default 값을 설정할 수 있다.
<!-- MyBtn.vue -->
<template>
  <button
    type="button"
    :style="styleObj"
    class="btn"
    :class="classObj">
    Banana
  </button>
</template>

<script>
export default {
  props: {
    bgColor: {
      type: String,
      default: 'gray'
    },
    color: {
      type: String,
      default: 'white'
    },
    large: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      styleObj: {
        backgroundColor: this.bgColor,
        color: this.color
      },
      classObj: {
        btn_large: this.large
      }
    }
  }
}
</script>

profile
UI 화면 만드는걸 좋아하는 UI개발자입니다. 프론트엔드 개발 공부 중입니다. 공부한 부분을 블로그로 간략히 정리하는 편입니다.

0개의 댓글