使用v-model实现子向父传值
一、父组件
<template>
<div>
<child v-model="inputValue"></child>
{{ inputValue }}
</div>
</template>
<script>
import child from "./child";
export default {
components: {
child,
},
data() {
return {
inputValue: "",
};
},
};
</script>
二、子组件
<template>
<div>
<input type="text" v-model="newValue" />
</div>
</template>
<script>
export default {
data() {
return {};
},
props: ["value"],
computed: {
newValue: {
get() {
return this.value;
},
set(value) {
return this.$emit("input", value);
},
},
},
};
</script>