Dynamically add wrapper element in vue3 composition api

321 Views Asked by At

Consider a component below, which accepts tag as props.


<template>
    <input v-model="model"/>
</template>
<script>
export default { 
    name: "InputComponent",
    props: {
        tag: {
            type: String,
            required: false,
            default: null,
        }
    }
}
</script>

I want passing the props div as tag value should return dom below.


<div>
   <input v-model="model"/>
</div>

Solution with composition api is advantange.

1

There are 1 best solutions below

3
On
<template>
    <component :is="tag">
        <input v-model="model"/>
    </component>
</template>
<script>
    export default { 
        name: "InputComponent",
        props: {
            tag: {
                type: String,
                required: false,
                default: null,
            }
        }
    }
</script>

should work.