티스토리 뷰

부모 컴포넌트에 정의되어 있는 method를 자식 컴포넌트에서 사용하고 싶을 때.

props로 해당 method를 받아 사용한다.

 

*Parent 컴포넌트

<template>
  <div>
    <ChildComponent
      :method-name-kebabCase="parentMethod"
    ></ChildComponent>
  </div>
</template>

<script>
import ChildComponent from './ChildComponent.vue'
export default {
  components: {
    ChildComponent
  },
  methods: {
    parentMethod(){
      return "text"
    }
  }
}
</script>

 

*Child 컴포넌트

<template>
  <div>
    <button
      :class="`button ${getParentMethod(parentMethod)}`"
      >getParentMethod(parentMethod)</button>
  </div>
</template>

<script>
export default {
  props: {
    getParentMethod: {
      type: Function,
      default: () => {}
    }
  }
}
</script>

 

 

 

 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
글 보관함