【问题标题】:Dynamically add wrapper element in vue3 composition api在 vue3 组合 api 中动态添加包装器元素
【发布时间】:2022-01-21 23:13:36
【问题描述】:

考虑下面的一个组件,它接受tag 作为道具。


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

我想将道具div 传递为tag 值应该返回下面的dom。


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

有composition api的解决方案是有优势的。

【问题讨论】:

    标签: vuejs3 vue-composition-api vue-functional-component


    【解决方案1】:
    <template>
        <component :is="tag">
            <input v-model="model"/>
        </component>
    </template>
    <script>
        export default { 
            name: "InputComponent",
            props: {
                tag: {
                    type: String,
                    required: false,
                    default: null,
                }
            }
        }
    </script>
    

    应该可以。

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    • 没有标签条件怎么办?
    • 您可以使用v-ifv-else 或将渲染函数作为prop(标签)传递,以渲染包装器
    猜你喜欢
    • 2021-06-24
    • 2021-04-27
    • 2021-07-21
    • 2022-12-14
    • 2021-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多