我创建了一个自定义输入
<template>
<div class="content">
<p>
<slot></slot>
</p>
<input v-model="content" class="textbox" :type="type" @input="handleInput">
</div>
</template>
<script>
export default {
name: 'vTextbox',
props:{
type: String,
},
data: function(){
return {
content: ""
}
},
methods:{
handleInput(){
this.$emit('input', this.content)
}
}
}
</script>父组件调用自定义输入组件以获取其内容,如:
<vTextbox v-model="email" type="email">Email</vTextbox>export default {
...
data: function(){
return{
email: "",
}
},
methods:{
Clear: function(){
this.email = ""
}
}
}当调用clear函数时,我希望清除自定义输入组件的值/内容。我尝试将this.email=设置为“”,但它不起作用。
发布于 2019-12-03 18:50:42
问题是您没有收到自定义输入中的值。虽然父组件中有v-model,但是要使v-model魔术发挥作用,组件需要实现value支柱并监视change。
以下是可能的样子
<template>
<div class="content">
<p>
<slot></slot>
</p>
<input v-model="content" class="textbox" :type="type" @input="handleInput">
</div>
</template>
<script>
export default {
name: 'vTextbox',
props:{
value: String, // added value prop
type: String,
},
data: function(){
return {
content: ""
}
},
watch:{
value(val) {
this.content = val; // added watch to override internal value, this will allow clear to work
}
},
methods:{
handleInput(){
this.$emit('input', this.content)
}
}
}
</script>https://stackoverflow.com/questions/59163163
复制相似问题