使用VUE中的道具从子模式数据中更新父数据

发布于 2025-02-03 00:20:12 字数 3062 浏览 3 评论 0原文

我正在开发发票,在那里我想使用模态添加新客户端,然后重新添加数据以创建发票页面(父页面)。 我试图遵循许多以前的问题,但仍然无法弄清楚,如何在此处正确使用$ emit。 如何将数据从模态传递到父。

这是到目前为止的代码。

这是createInvoice.vue

<template>
   <button @click="isComponentModalActive = true">
       Add New Client
   </button>

   <b-modal
      v-model="isComponentModalActive"
       has-modal-card
       full-screen
       :can-cancel="false"
    >
      <client-add-form v-bind="form.clientData"></client-add-form>
    </b-modal>
</template>

<script>
import ClientAddForm from '../../../components/ClientAddForm.vue';
export default {
  components: { ClientAddForm },
    data() {
    return {
      isComponentModalActive: false,
      form: {
        clientData: {
          name: "Gaurav Kumar",
          email: "[email protected]",
          phone: "",
          address: "",
          city: "",
          country: "",
          taxCode: "",
          Type: "",
        },
       }
}
</script>

这是clientaddform.vue模态

    <template>
      <div class="modal-card" style="width: auto">
        <header class="modal-card-head">
          <p class="modal-card-title">Add/Modify Customer Information</p>
        </header>
        <section class="modal-card-body">
          <b-field label="Name">
            <b-input type="text" :value="name" placeholder="Name"> </b-input>
          </b-field>
          <b-field label="Phone">
            <b-input type="phone" :value="phone" placeholder="Phone"> </b-input>
          </b-field>
          <b-field label="Email">
            <b-input type="email" :value="email" placeholder="Email"> </b-input>
          </b-field>
          <b-field label="Address">
            <b-input type="textarea" :value="address" placeholder="Address">
            </b-input>
          </b-field>
          <b-field label="City">
            <b-input type="text" :value="city" placeholder="City"> </b-input>
          </b-field>
          <b-field label="Country">
            <b-input type="text" :value="country" placeholder="Country"> </b-input>
          </b-field>
        </section>
        <footer class="modal-card-foot">
          <b-button label="Close" @click="$parent.close()" />
          <b-button label="Save" type="is-primary" @click="Update()"  />
        </footer>
      </div>
   </template>
    
    
  <script>
    export default {
      props: ["email", "phone", "name", "city", "country", "address"],
    
      methods: {
        Update(){
           //Database Operations etc
           this.$emit()
        }
      }
    }
 </script>

I am working on invoice, where i want to add new client using modal and then get added data back to create invoice page (Parent Page).
I have tried to follow many previous questions but still not able to figure it out, how to properly use $emit here.
How to Pass data from Modal to Parent..

Here are the codes so far.

this is createInvoice.vue

<template>
   <button @click="isComponentModalActive = true">
       Add New Client
   </button>

   <b-modal
      v-model="isComponentModalActive"
       has-modal-card
       full-screen
       :can-cancel="false"
    >
      <client-add-form v-bind="form.clientData"></client-add-form>
    </b-modal>
</template>

<script>
import ClientAddForm from '../../../components/ClientAddForm.vue';
export default {
  components: { ClientAddForm },
    data() {
    return {
      isComponentModalActive: false,
      form: {
        clientData: {
          name: "Gaurav Kumar",
          email: "[email protected]",
          phone: "",
          address: "",
          city: "",
          country: "",
          taxCode: "",
          Type: "",
        },
       }
}
</script>

this is ClientAddForm.vue Modal

    <template>
      <div class="modal-card" style="width: auto">
        <header class="modal-card-head">
          <p class="modal-card-title">Add/Modify Customer Information</p>
        </header>
        <section class="modal-card-body">
          <b-field label="Name">
            <b-input type="text" :value="name" placeholder="Name"> </b-input>
          </b-field>
          <b-field label="Phone">
            <b-input type="phone" :value="phone" placeholder="Phone"> </b-input>
          </b-field>
          <b-field label="Email">
            <b-input type="email" :value="email" placeholder="Email"> </b-input>
          </b-field>
          <b-field label="Address">
            <b-input type="textarea" :value="address" placeholder="Address">
            </b-input>
          </b-field>
          <b-field label="City">
            <b-input type="text" :value="city" placeholder="City"> </b-input>
          </b-field>
          <b-field label="Country">
            <b-input type="text" :value="country" placeholder="Country"> </b-input>
          </b-field>
        </section>
        <footer class="modal-card-foot">
          <b-button label="Close" @click="$parent.close()" />
          <b-button label="Save" type="is-primary" @click="Update()"  />
        </footer>
      </div>
   </template>
    
    
  <script>
    export default {
      props: ["email", "phone", "name", "city", "country", "address"],
    
      methods: {
        Update(){
           //Database Operations etc
           this.$emit()
        }
      }
    }
 </script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南汐寒笙箫 2025-02-10 00:20:12

您发出事件并传递数据this。$ emit('更新',this.client)
在父组件中,您可以从Child和Trigger Method Method @Updated =“ handleupdate”

请查看以下片段PLS:

Vue.component('client-add-form', {
  template: `
    <div class="modal-card" style="width: auto">
      <header class="modal-card-head">
        <p class="modal-card-title">Add/Modify Customer Information</p>
      </header>
      <section class="modal-card-body">
        <b-field label="Name">
          <b-input type="text" v-model="client.name" placeholder="Name"> </b-input>
        </b-field>
        <b-field label="Phone">
          <b-input type="phone" v-model="client.phone" placeholder="Phone"> </b-input>
        </b-field>
        <b-field label="Email">
          <b-input type="email" v-model="client.email" placeholder="Email"> </b-input>
        </b-field>
        <b-field label="Address">
          <b-input type="textarea" v-model="client.address" placeholder="Address">
          </b-input>
        </b-field>
        <b-field label="City">
          <b-input type="text" v-model="client.city" placeholder="City"> </b-input>
        </b-field>
        <b-field label="Country">
          <b-input type="text" v-model="client.country" placeholder="Country"> </b-input>
        </b-field>
      </section>
      <footer class="modal-card-foot">
        <b-button label="Close" @click="$parent.close()" />
        <b-button label="Save" type="is-primary" @click="update" />
      </footer>
    </div>
  `,
  props: ["formData"],
  data() {
    return {
      client: {...this.formData}
    }
  },
  methods: {
    update(){
       //Database Operations etc
       this.$emit('updated', this.client)
       this.$parent.close()
    }
  },
})

new Vue({
  el: '#demo',
  data() {
    return {
      isComponentModalActive: false,
      form: {
        clientData: {
          name: "Gaurav Kumar",
          email: "[email protected]",
          phone: "",
          address: "",
          city: "",
          country: "",
          taxCode: "",
          Type: "",
        },
      }
    }
  },
  methods: {
    handleUpdate(client) {
      this.form.clientData = client
    }
  }
})
<link rel="stylesheet" href="https://unpkg.com/buefy/dist/buefy.min.css">
<script src="https://unpkg.com/vue@2"></script>
<script src="https://unpkg.com/buefy/dist/buefy.min.js"></script>
<script src="https://unpkg.com/buefy/dist/components/table"></script>
<script src="https://unpkg.com/buefy/dist/components/input"></script>
<div id="demo">
  <button @click="isComponentModalActive = true">
    Add New Client
  </button>
  <b-modal
     v-model="isComponentModalActive"
     has-modal-card
     full-screen
     :can-cancel="false"
  >
    <client-add-form 
      :form-data="form.clientData" 
      @updated="handleUpdate"
    ></client-add-form>
    </b-modal>
    {{ form }}
</div>

You emit event and pass data this.$emit('updated', this.client)
In parent component you listen to event from child and trigger method @updated="handleUpdate"

Take a look at following snippet pls:

Vue.component('client-add-form', {
  template: `
    <div class="modal-card" style="width: auto">
      <header class="modal-card-head">
        <p class="modal-card-title">Add/Modify Customer Information</p>
      </header>
      <section class="modal-card-body">
        <b-field label="Name">
          <b-input type="text" v-model="client.name" placeholder="Name"> </b-input>
        </b-field>
        <b-field label="Phone">
          <b-input type="phone" v-model="client.phone" placeholder="Phone"> </b-input>
        </b-field>
        <b-field label="Email">
          <b-input type="email" v-model="client.email" placeholder="Email"> </b-input>
        </b-field>
        <b-field label="Address">
          <b-input type="textarea" v-model="client.address" placeholder="Address">
          </b-input>
        </b-field>
        <b-field label="City">
          <b-input type="text" v-model="client.city" placeholder="City"> </b-input>
        </b-field>
        <b-field label="Country">
          <b-input type="text" v-model="client.country" placeholder="Country"> </b-input>
        </b-field>
      </section>
      <footer class="modal-card-foot">
        <b-button label="Close" @click="$parent.close()" />
        <b-button label="Save" type="is-primary" @click="update" />
      </footer>
    </div>
  `,
  props: ["formData"],
  data() {
    return {
      client: {...this.formData}
    }
  },
  methods: {
    update(){
       //Database Operations etc
       this.$emit('updated', this.client)
       this.$parent.close()
    }
  },
})

new Vue({
  el: '#demo',
  data() {
    return {
      isComponentModalActive: false,
      form: {
        clientData: {
          name: "Gaurav Kumar",
          email: "[email protected]",
          phone: "",
          address: "",
          city: "",
          country: "",
          taxCode: "",
          Type: "",
        },
      }
    }
  },
  methods: {
    handleUpdate(client) {
      this.form.clientData = client
    }
  }
})
<link rel="stylesheet" href="https://unpkg.com/buefy/dist/buefy.min.css">
<script src="https://unpkg.com/vue@2"></script>
<script src="https://unpkg.com/buefy/dist/buefy.min.js"></script>
<script src="https://unpkg.com/buefy/dist/components/table"></script>
<script src="https://unpkg.com/buefy/dist/components/input"></script>
<div id="demo">
  <button @click="isComponentModalActive = true">
    Add New Client
  </button>
  <b-modal
     v-model="isComponentModalActive"
     has-modal-card
     full-screen
     :can-cancel="false"
  >
    <client-add-form 
      :form-data="form.clientData" 
      @updated="handleUpdate"
    ></client-add-form>
    </b-modal>
    {{ form }}
</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文