몽땅뚝딱 개발자

[Vue.js] Vue3에서 빌트인 컴포넌트 <component> 사용하기 본문

Development/Vue.js

[Vue.js] Vue3에서 빌트인 컴포넌트 <component> 사용하기

레오나르도 다빈츠 2022. 6. 25. 13:21

 

 

[Vue.js] 빌트인 컴포넌트 - component와 is

Vue의 Built-In Components ◽ component 이 컴포넌트는 Build-In Component로 동적 컴포넌트를 렌더링 하기 위한 '메타 컴포넌트'라고 한다. 렌더링 할 실제 컴포넌트는 is prop에 의해 결정된다. ◽ is 속성 is..

be-a-weapon.tistory.com

 

Vue2로 개발할 때는 <component>를 한번도 사용해보지 않아서 차이점은 잘 모르겠지만..

Vue3부터 <component>를 사용하는 방법이 바뀌었다고 한다.

 

 


 

📄 HTML

<template>
	...
	<!-- for문 안에서 컴포넌트 이름을 가져와서 선언했다. -->
	<component :is="changeTab(item.type)" :info="item"></component>
	...
</template>

 

 

📄 Script

<script setup>
import { markRaw, ref } from 'vue'
import ChildComp1 from '@/components/ChildComp1.vue'
import ChildComp2 from '@/components/ChildComp2.vue'

const targetComponent = ref(null)
const changeTab = (component: string) => {
  const components: any = {
    ChildComp1,
    ChildComp2,
  }
  return (targetComponent.value = markRaw(components[component]))
}
</setup>

 

 


 

 

출처

 

How to use <component :is=""> in vue 3 script setup

I am using the experimental script setup to create a learn enviroment. I got a selfmade navigation bar with open a single component. I am having trouble using the <component :is="" />

stackoverflow.com

 

 


개인적으로 공부한 내용을 정리하는 블로그로
잘못된 개념을 게시하지않도록 주의하고 있으나 오류가 있을 수 있습니다.

 

 

Comments