1

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>

2 Answers 2

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>

Sign up to request clarification or add additional context in comments.

2 Comments

hi thank you for your answer ! this wasnt what i was looking for, I would like sometimes to display a p tag instead of a h1 tag with the same text content, i guess we need to do a if else condition
You could do <MyComponent tag="p" />
0

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/>

2 Comments

hi thank you for your answer, i think your answer refers to a prop not displaying a html tag !
@fancy56 No my answer is a proper way to change component content without making a mess

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.