【发布时间】:2022-02-28 05:20:24
【问题描述】:
我正在使用Vuetify(v2.6.3) 构建一个新的/编辑项目表单。我的表单有多个组合框,它们从后端 API 中提取它们的项目作为对象。我希望能够添加组合框能够执行的新项目。但是,当我添加一个新项目时,它会作为字符串添加。有没有办法将新项目添加为对象?
我只需要新对象有一个name 密钥,以便将其发送到要创建的 API
来自v-combobox 的所需New 项目:
{
name: 'New Item',
}
从 API 返回并可作为组合框选择的项目的简化列表:
[
{
id: 1,
name: 'Item 1',
createdAt: '2020-01-01',
updatedAt: '2020-01-01'
},
{
id: 2,
name: 'Item 2',
createdAt: '2020-01-01',
updatedAt: '2020-01-01'
},
{
id: 3,
name: 'Item 3',
createdAt: '2020-01-01',
updatedAt: '2020-01-01'
}
]
这是我的表单的简化版本:
<template>
<v-card>
<v-card-title>
<span class="text-h5">Edit Item</span>
</v-card-title>
<v-card-text>
<v-container>
<v-row>
<v-col>
<v-combobox
v-model="item"
:items="items"
item-text="name"
item-value="id"
label="ComboBox"
return-object
></v-combobox>
</v-col>
</v-row>
</v-container>
</v-card-text>
</v-card>
</template>
<script>
export default {
name: 'TestForm',
data() {
return {
item: null,
items: [
{
id: 1,
name: 'Item 1',
createdAt: '2020-01-01',
updatedAt: '2020-01-01',
},
{
id: 2,
name: 'Item 2',
createdAt: '2020-01-01',
updatedAt: '2020-01-01',
},
{
id: 3,
name: 'Item 3',
createdAt: '2020-01-01',
updatedAt: '2020-01-01',
},
],
}
},
}
</script>
在将表单发送到 API 的方法中,我可以检查每个组合框的值并在需要时转换为对象,但我想知道是否可以在组合框组件中处理它。
【问题讨论】:
标签: node.js vue.js nuxt.js vuetify.js