如何将输入数据值从子组件数据对象发送到父组件?

时间:2020-06-21 15:50:32

标签: vue.js vue-component

我正在尝试通过dataobject'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执行提交操作的帮助。如果还有其他更好的方法,请告诉我。谢谢。

2 个答案:

答案 0 :(得分:0)

首先,您应该将最多两个参数传递给$ emit方法,这里是文档:https://vuejs.org/v2/api/#vm-emit,第二个是v-on:,然后再添加v模型。 因此,您可以在一个对象而不是三个数据中传递此数据的解决方案,因此代码将如下所示:


      data() {
          return {
            name: '',
            email: '',
            age: '',
          }
      },
      methods: {
            inputdata() {
              this.$emit("input", {
                name: this.name, 
                email: this.email, 
                age: this.age
              })
            }
      }

或我的首选选项将所有内容都放入这样的表单数据中

<template>
    <div>
  <table>
    <thead>
    <th>Name</th>
    <th>Email</th>
    <th>Age</th>
    </thead>
    <tr>
      <td><input type="text" id="name" v-model="form.name"></td>
      <td><input type="email" id="email" v-model="form.email"></td>
      <td><input type="number" id="age" v-model="form.age"></td>
    </tr>
  </table>
    </div>
</template>

<script>
    export default {
        name: "inputform",
      data() {
          return {
            form: {
              name: '',
              email: '',
              age: '',
            }
          }
      },
      methods: {
            inputdata() {
              this.$emit("input", this.form)
            }
      }
    }
</script>

答案 1 :(得分:0)

附加v-model时不需要v-on。您也可以像这样将详细信息捕获到单个对象中,然后将其作为发出的事件的一部分传递。

子组件

<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">
        </td>
        <td>
          <input type="email" id="email" v-model="details.email">
        </td>
        <td>
          <input type="number" id="age" v-model="details.age">
        </td>
        <td>
          <button @click="inputdata">Submit</button>
        </td>
      </tr>
    </table>
  </div>
</template>

<script>
export default {
  name: "inputform",
  data() {
    return {
      details: {
        name: "",
        email: "",
        age: ""
      }
    };
  },
  methods: {
    inputdata() {
      console.log(this.details);
      this.$emit("handledata", this.details);
    }
  }
};
</script>

父组件

<template>
  <div id="app">
    <HelloWorld v-on:handledata="handleInput"/>
  </div>
</template>

<script>
import HelloWorld from "./components/HelloWorld";

export default {
  name: "App",
  components: {
    HelloWorld
  },
  methods: {
    handleInput(data) {
      // object emitted from the child component
      console.log({ data });
    }
  }
};
</script>
相关问题