![](/img/trans.png)
[英]How can I pass v-model from a child component to parent component in vue?
[英]It's there any way to get v-model's variable name in the parent component from a child component
对于下面的情况,当我自定义无线电的vue组件时,我可以使用模型选项来获取v-model值,该值应为字符串'1'。如何在子节点中获取其变量名称'radio1'? 它有什么办法吗?
儿童组成部分
<template>
<input type="radio" v-model="prop" :value="value">
</template>
<script>
export default {
model: {
prop: "prop"
},
props: {
prop: {
default: ''
},
value: {
default: ''
}
}
}
</script>
父级使用此组件
<template>
<div>
<radio-component v-model="radio1" value="1"></radio-component>
<radio-component v-model="radio1" value="2"></radio-component>
</div>
</template>
<script>
import radioComponent from './radio'
export default {
components: {
radioComponent
},
data () {
return {
radio1: '1'
}
}
}
</script>
您可以使用您想要的任何名称发出事件,因此在您可以编写的子项中:
<input type="radio" @input="$emit('prop', prop)" v-model="prop" :value="value">
然后在父母中你可以做到:
<radio-component v-model="radio1" @prop="doSomething" value="1" name="nameValue"></radio-component>
.
.
.
data(){
return {
nameValue: null
}
methods: {
doSomthing(prop){
this.nameValue = prop
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.