使用VUE中的道具从子模式数据中更新父数据
我正在开发发票,在那里我想使用模态添加新客户端,然后重新添加数据以创建发票页面(父页面)。 我试图遵循许多以前的问题,但仍然无法弄清楚,如何在此处正确使用$ 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您发出事件并传递数据
this。$ emit('更新',this.client)
在父组件中,您可以从Child和Trigger Method Method
@Updated =“ handleupdate”
请查看以下片段PLS:
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: