【发布时间】:2020-10-11 12:26:52
【问题描述】:
我正在尝试通过数据对象“data()”将输入数据变量从子组件表单发送到父组件。我看过vuejs update parent data from child component 文章并尝试这样做,但是我无法通过事件 $emit 捕获的数据对象。你能帮帮我吗?
父组件:
<script>
import inputform from '../components/form.vue';
import axios from 'axios';
export default {
name: 'helloword',
data() {
return {
}
},
components: {
inputform
},
methods: {
submit() {
const path = 'http://127.0.0.1:5000';
axios.post(path, {
name: inputform.data()
})
.then(() => {
const result = 'Sucess!';
console.log(result);
})
.catch((error) => {
console.log(error);
})
}
}
}
</script>
子组件:
<template>
<div>
<table>
<thead>
<th>Name</th>
<th>Email</th>
<th>Age</th>
</thead>
<tr>
<td><input type="text" id="name" v-model="details.name" @focusout="inputdata"></td>
<td><input type="text" id="name1" v-model="details.name1" @focusout="inputdata" ></td>
<td><input type="number" id="age" v-model="details.age" @focusout="inputdata" ></td>
</tr>
</table>
</div>
</template>
<script>
export default {
name: "inputform",
data() {
return {
details: {
name: '',
name1: '',
age: ''
}
}
},
methods: {
inputdata() {
this.$emit("input_data", this.details)
}
}
}
</script>
<style scoped>
</style>
因此,寻求帮助,将可变数据从子组件发送到父组件,并使用父组件的 axios 向 API 执行提交操作。如果有其他更好的方法请告诉我。谢谢。
【问题讨论】:
标签: vue.js vue-component