[英]best alternative for <slot/> in <textarea> in [ Vue.js 3 ] component
I want create component with textarea and pass data inside that like我想用 textarea 创建组件并在其中传递数据
<c-textarea> hello world </c-textarea>
but the classic <slot/>
tag not work inside of textarea what's simplest and cleanest alternative但是经典的
<slot/>
标签在 textarea 内部不起作用什么是最简单和最干净的选择
<template>
<textarea><slot/></textarea>
</template>
in Vue.js 3在 Vue.js 3
You should use value
& input
to bind the content instead of using slot
您应该使用
value
& input
来绑定内容而不是使用slot
Here is the updated version of CTextarea
component这是
CTextarea
组件的更新版本
<template>
<textarea :value="modelValue" @input="$emit('update:modelValue', $event.target.value)">
</textarea>
</template>
<script>
export default {
name: 'CTextarea',
emits: ['update:modelValue'],
props: {
modelValue: String,
},
};
</script>
check this woking demo检查这个唤醒演示
You can extract the content of a slot:您可以提取插槽的内容:
<template>
<textarea>{{ $slots.default ? $slots.default()[0].children : ''}}</textarea>
</template>
Basically, this builds the slot manually, which gives you a VNode element, where children
contains the slot content.基本上,这会手动构建插槽,为您提供一个 VNode 元素,其中
children
元素包含插槽内容。
I would really try to find another way though, this is coarse, error prone and most likely not what you want to do.不过,我真的会尝试找到另一种方法,这是粗略的、容易出错的,而且很可能不是您想做的。
Personally, I would stick to the v-model
approach.就个人而言,我会坚持使用
v-model
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.