vue 如何从子组件访问 v-model [英] vue how to access v-model from child component
本文介绍了vue 如何从子组件访问 v-model的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的子组件是这样的
然后我像这样从内部访问组件
<div><input :value="this.value">
</模板><脚本>导出默认{数据() {返回 {值:this.edit_thread.body}}}
不工作,我想念什么?
解决方案
To 在自定义组件上使用v-model
,组件需要:
</模板><脚本>导出默认{道具:['价值']}
和 发出一个 <代码>输入代码>事件带有新值:
<div><input @input="$emit('input', $event.target.value)">
</模板>
my child component is like this
<editor v-model="edit_thread.body"></editor>
and then I access the component from inside like this
<template>
<div>
<input :value="this.value">
</div>
</template>
<script>
export default {
data() {
return {
value: this.edit_thread.body
}
}
}
</script>
not working, I miss something?
解决方案
To use v-model
on custom components, the component needs to:
have a
prop
(not adata
property) namedvalue
:<template> <div> <input :value="value"> </div> </template> <script> export default { props: ['value'] } </script>
and emit an
input
event with new values:<template> <div> <input @input="$emit('input', $event.target.value)"> </div> </template>
这篇关于vue 如何从子组件访问 v-model的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!