我知道您可以使用v-model
将值绑定到同一组件中的输入。如何为输入创建包装器组件并将其绑定值?
Login.vue
<template>
<div id="Login">
<Input v-bind:value="email"/>
<Input v-bind:value="password"/>
</div>
</template>
<script>
import Input from './Input.vue'
import Button from './Button'
export default {
name: 'Login',components: {
Input,Button,},data: () => ({
email:'test',password:'test',}),methods: {
login: () => { debugger; },//this.email and this.password are still set to test
}
}
</script>
Input.vue
<template>
<div class="input>
<input v-model="value"/>
</div>
</template>
<script>
export default {
name: 'Input',props: {
value: String,}
</script>
当前设置结果
[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead,use a data or computed property based on the prop's value. Prop being mutated: "value"
通过发出事件来做到这一点的唯一方法吗?