简体   繁体   English

this.$emit 在 Vuejs 中将值从子组件传递给父组件时不起作用

[英]this.$emit not working while passing the value from child to parent component in Vuejs

The method listening to the event called by this.$emit is not getting called and I am not able to retrieve the value from child to parent component in Vuejs.监听this.$emit调用的事件的方法没有被调用,我无法在 Vuejs 中从子组件检索到父组件的值。

The Contact.vue is the parent component where I need to display the value that I am capturing from the ContactDetails child component through a form. Contact.vue 是父组件,我需要在其中显示我通过表单从 ContactDetails 子组件捕获的值。

<template>
  <div>
    <h3>Contact Details</h3>
    <br /><br />
    <form>
      <div class="form-group">
        <label>Business Name</label>
        <input
          type="text"
          class="form-control"
          v-model="contact.businessName"
        />
      </div>
      <div class="form-group">
        <label>Contact Person</label>
        <input
          type="text"
          class="form-control"
          v-model="contact.contactPerson"
        />
      </div>
      <div class="form-group">
        <label>Address</label>
        <textarea
          type="text"
          class="form-control"
          v-model="contact.address"
        ></textarea>
      </div>
      <div class="form-group">
        <label>Contact Number</label>
        <input
          type="text"
          class="form-control"
          v-model="contact.contactNumber"
        />
      </div>
      <div class="form-group">
        <label>Category of Business</label>
        <select id="inputState" class="form-control">
          <option
            v-for="category in category"
            :key="category.categoryCode"
            :selected="(contact.selectedCategory = category.categoryCode)"
            >{{ category.categoryText }}</option
          >
        </select>
      </div>
      <button class="btn btn-primary" @click.prevent="submitRequest">
        Submit Contact
      </button>
    </form>
  </div>
</template>
<script>
export default {
  data() {
    return {
      contact: {
        businessName: "",
        contactPerson: "",
        address: "",
        contactNumber: "",
        selectedCategory: ""
      },
      category: [
        {
          categoryText: "Food",
          categoryCode: 1
        },
        {
          categoryText: "Medicine",
          categoryCode: 2
        },
        {
          categoryText: "Shop",
          categoryCode: 3
        },
        {
          categoryText: "Housekeeping",
          categoryCode: 4
        }
      ]
    };
  },
  methods: {
    submitRequest() {
console.log(this.contact);
      this.$emit("submittedContact", this.contact.businessName);
    }
  }
};
</script>
<style scoped></style>

<template>
  <div>
    <contact-detail @submittedContact="contact = $event"></contact-detail>
    <contact-summary> </contact-summary>
  </div>
</template>
<script>
import ContactDetails from "./ContactDetails.vue";
import ContactSummary from "./ContactSummary.vue";
export default {
  data() {
    return {
      contact: {
        businessName: "",
        contactPerson: "",
        address: "",
        contactNumber: "",
        selectedCategory: ""
      }
    };
  },
  methods: {
    submittedContact() {
      console.log(this.contact);
    }
  },
  components: {
    "contact-detail": ContactDetails,
    "contact-summary": ContactSummary
  }
};
</script>

Mentioned above are the code for the parent component and the child component.上面提到的是父组件和子组件的代码。

Change this to:将其更改为:

<contact-detail @submittedContact="contact = $event"></contact-detail>

to

<contact-detail @submittedContact="submittedContact"></contact-detail>

@submittedContact means your parent is listening for an event named submittedContact from your component contact-detail and "submittedContact" refers to the method it will call when it hears that event. @submittedContact表示您的父级正在从您的组件contact-detail中侦听名为 submitContact 的event ,而“ submittedContact "submittedContact"指的是它在听到该事件时将调用的方法。

and change your method body to:并将您的方法主体更改为:

  methods: {
    submittedContact(contact) {
      // and maybe here you can set this.contact = { ...contact }
      console.log(contact);
    }
  },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 this.$emit() 不是 function,同时尝试在 Vue 3 中更改子组件中的子组件中的 state API - this.$emit() is not a function while trying to change state in parent component from child in composition API in vue 3 无法从Vue.js的子组件向父组件发出事件 - Not able to emit an event to a parent component from child component in Vuejs VueJS如何从子组件向其父组件发出事件 - VueJS How to emit an event from a child component to its parent component VueJs:如何使用 $emit(optionalPayload) 和 $on 将“计算函数”值从子组件传递到父组件? - VueJs: How to pass a 'computed function' value from child component to the parent component using $emit(optionalPayload) and $on? 从子组件向父组件发出值 - Emit value from child to parent component 如何在 vuejs 的父组件中从子组件向 object 发出值? - How to Emit values from child to an object in parent component in vuejs? VueJS-从子组件到父组件的通信不起作用 - VueJS - Communicating from child component to parent not working Vuejs 将不工作的形式从子级发送到父级 - Vuejs emit not working form child to parent vuejs中父组件传递给子组件的方法 - Passing method from parent component to child component in vuejs Vue-将数据从子级传递到父级-基本发射不起作用 - Vue - Passing data from child to parent - Basic emit not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM