[英]Vue parent-child emit function is breaking v-model binding
Working on fixing a bug in someone else's code, so I'm trying to limit what I have to change here.正在修复其他人代码中的错误,所以我试图限制我在这里必须更改的内容。
Seems like when I use $emit
functionality to run functions between child and parent components, v-model binding is being lost in my components.似乎当我使用
$emit
功能在子组件和父组件之间运行函数时,我的组件中丢失了 v-model 绑定。
There is a parent component:有一个父组件:
ParentComponent.vue
父组件.vue
<template>
<child-component v-bind:items="this.items"
v-on:event_child="this.eventUpdater">
</child-component>
<template>
<script>
import ChildComponent from './ChildComponent.vue';
export default {
components: {
'child-component': ChildComponent
},
methods: {
getItemDetails() {
//...ajax request that loads item details for page.
},
eventUpdater: function(id) {
this.getItemDetails();
}
}
}
</script>
Then, there is a child component:然后,有一个子组件:
ChildComponent.vue
子组件.vue
<template>
<div v-for="item in items">
<input v-model="item.itemId">
</div>
<button v-on:click="updateItems">update</button>
</template>
<script>
export default {
props: ['items'],
methods: {
updateItems() {
//...ajax call that updates items.
this.emitWhat();
},
emitWhat: function () {
this.$emit('event_child');
}
}
}
</script>
After updating my initial item (which updates fine), I go to update another item, and it seems like the v-model for that item does not work.更新我的初始项目(更新正常)后,我去更新另一个项目,似乎该项目的 v-model 不起作用。 Is the
$emit
functionality breaking the v-model binding after initially loading? $emit
功能是否在初始加载后破坏了 v-model 绑定? How do I fix this?我该如何解决?
You are using this:你正在使用这个:
<child-component v-bind:items="this.items"
v-on:event_child="this.eventUpdater">
but should use this:但应该使用这个:
<child-component v-bind:items="items"
v-on:event_child="eventUpdater">
Removed this.
删除了
this.
. .
Also I didn't find items
as data
property in parent component.我也没有在父组件中找到作为
data
属性的items
。
Upd.更新。
Also if you define id
parameter in eventUpdater: function(id)
method you should emit
it like this:此外,如果您在
eventUpdater: function(id)
方法中定义id
参数,您应该像这样emit
它:
<template>
<div v-for="item in items">
<input v-model="item.itemId">
</div>
<button v-on:click="updateItems(item.itemId)">update</button>
</template>
updateItems(id) {
//...ajax call that updates items.
this.emitWhat(id);
},
emitWhat: function (id) {
this.$emit('event_child', id);
}
Upd.2更新 2
Also you have v-model on item.itemId
which could be a problem:另外你在
item.itemId
上有 v-model 这可能是一个问题:
<input v-model="item.itemId">
You could consider to bind v-model
to newItems
like this:您可以考虑像这样将
v-model
绑定到newItems
:
<input v-model="newItems[itemId]">
data(){
return {
newItems: [],
//...
};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.