【发布时间】:2020-02-21 19:42:15
【问题描述】:
我正在克隆一个数组并使用map 为数组中的每个对象添加一个属性。出于某种原因,当我这样做时,添加的属性在v-model 中使用时没有更新。任何想法为什么会发生这种情况?
在下面的代码中,在文本字段中键入不会更新项目数量。如何更改代码才能做到这一点?
<template>
<v-data-table
:headers="headers"
:items="items"
>
<template v-slot:item.quantity="props">
<v-text-field v-model="props.item.quantity"></v-text-field>
</template>
</v-data-table>
</template>
<script>
export default {
props: {
customer: {
type: Object,
required: true
}
},
data: () => ({
headers: [
{ text: 'ID', value: 'id' },
{ text: 'Description', value: 'description' },
{ text: 'Quantity', value: 'quantity' },
],
items: []
}),
created() {
this.initialize()
},
methods: {
initialize() {
const items = [...this.customer.items]
items.map(item => {
item.quantity = ''
})
this.items = items
}
}
</script>
客户是来自 API 的 JSON 响应
{
"id": 1,
"name": "Customer",
"items": [
{
"id": 1,
"product": 1707,
"contract": null,
"plu": "709000",
"description": "ABC",
"unit": 1,
"price": "5.20"
}
],
}
更新
Link to codepen - 在qty2 字段中键入不会更新数据,因为它是使用map 添加到对象中的。
【问题讨论】:
-
试试
v-slot:item.quantity="{ item }"v-model="item.quantity"。 -
您能否分享this.customer.items的示例对象,有问题,这真的有助于解决您的问题
-
@azeós 也不起作用
-
@chans 问题已更新
-
当您更改文本框中的值时,上述方法将更新对象
标签: javascript vue.js vuejs2 vuetify.js