[英]Vuejs Submit form with inputs in child components
I have a parent component that is a form component that has multiple child components that contain input fields 我有一个父组件,它是一个表单组件,具有多个包含输入字段的子组件
<template>
<div class="form">
<generalData v-model="input" />
<textAreas v-model="input"/>
<button class="btn" @click="Submit()">Submit</button>
<div/>
</template>
<script>
export default {
data(){
return {
input: {
name: '',
age: '',
address: ''
bio: ''
}
}
},
methods: {
Submit(){
console.log('Submitting...');
console.log(this.input);
}
}
}
</script>
and the child components contain the text fields 子组件包含文本字段
<template>
<div class="generalData">
<input name="name" type="text" v-bind:value="input.name" v-on:input="updateInput($event.target.value)">
<input name="age" type="text" v-bind:value="input.age" v-on:input="updateInput($event.target.value)">
<div/>
</template>
<script>
export default {
props: ['input'],
data(){
return {
}
},
methods: {
updateInput(value){
this.$emit('input', value);
}
}
}
</script>
same for the other child component but the values are not getting updated and i am not able to submit them 其他子组件相同,但值没有更新,我无法提交它们
You have to use prop that you defined inside of your component. 您必须使用在组件内部定义的prop。 You named that prop input, then: 您为道具输入命名,然后:
<template>
<div class="form">
<generalData :input="input" />
<textAreas :input="input" />
<button class="btn" @click="submit">Submit</button>
<div/>
</template>
<script>
export default {
data(){
return {
input: {
name: '',
age: '',
address: ''
bio: ''
}
}
},
methods: {
submit(){
console.log('Submitting...');
console.log(this.input);
}
}
}
</script>
Try this code: 试试这个代码:
<template>
<div class="generalData">
<input type="text" v-model="person.name" @change="handleChange" @input="handleInput">
<input type="text" v-model="person.age" @change="handleChange" @input="handleInput">
<input type="text" v-model="person.address" @change="handleChange" @input="handleInput">
<input type="text" v-model="person.bio" @change="handleChange" @input="handleInput">
<div/>
</template>
<script>
export default {
props: ['value'],
data () {
return {
person: {
name: '',
age: '',
address: ''
bio: ''
}
}
},
methods: {
handleChange () {
return this.$emit('change', this.person)
},
handleInput () {
return this.$emit('input', this.person)
},
setCurrentValue (person) {
this.person = person
}
},
watch: {
value (val) {
if (!val) return
this.setCurrentValue(val)
}
}
}
</script>
Another way to approach this would be to make one input component and then iterate over the user object passing in an attribute and having v-model
directly on the input bound to the value. 解决此问题的另一种方法是制作一个输入组件,然后遍历传递属性的用户对象,并将v-model
直接绑定到与值绑定的输入上。 If your user object is in your root view instance you can access data using the this.$root.whatEver
如果您的用户对象位于根视图实例中,则可以使用this.$root.whatEver
访问数据
Check out this fiddle https://jsfiddle.net/caseyjoneal/urgantzo/113/ 看看这个小提琴https://jsfiddle.net/caseyjoneal/urgantzo/113/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.