Reputation: 11
I have a component which has a <p>
tag inside, but would like it to be a <h1>
tag sometimes, how to pass the prop ?
<template>
<p>Hello world</p>
</template>
Upvotes: 1
Views: 412
Reputation: 1485
Would recommend using slot for that component, which will be something like this
<template>
<slot name="content"></slot>
</template>
When you use your component you can just do this
<Component>
<template #content>
Your content here, whatever you like
<template/>
<Component/>
Upvotes: 0
Reputation: 1
Pass it as prop then use component
to render it :
<template>
<component :is="tag">Hello world</component >
</template>
<script>
export default{
name:'MyComponent',
props:['tag']
}
</script
then use the component like <MyComponent tag="h1" />
You could make MyComponent
more dynamic accepting any content by using slots :
<template>
<component :is="tag">{{msg}}</component >
</template>
<script>
export default{
name:'MyComponent',
props:['tag','msg']
}
</script
then use it like <MyComponent tag="h1" >hello world</MyComponent>
Upvotes: 1