首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >填充V-select from json

填充V-select from json
EN

Stack Overflow用户
提问于 2021-05-12 16:20:56
回答 1查看 36关注 0票数 0

我在填充我从后端收到的JSON中的v-select时遇到了问题。我对vuetify是个新手,网上分享的一些解决方案让我很困惑。

这是我的JSON

代码语言:javascript
复制
{
        "id": 2,
        "quantityOnHand": 7,
        "idealQuantity": 10,
        "product": {
            "id": 2,
            "createOn": "0001-01-01T00:00:00",
            "updateOn": "0001-01-01T00:00:00",
            "name": "10LB Dark Roast",
            "description": "10LB of Dark Roast Coffee Beans",
            "price": 67.0,
            "isTaxable": true,
            "isArchived": false
        }
    },
    {
        "id": 1,
        "quantityOnHand": 48,
        "idealQuantity": 10,
        "product": {
            "id": 1,
            "createOn": "0001-01-01T00:00:00",
            "updateOn": "0001-01-01T00:00:00",
            "name": "10LB Light Roast",
            "description": "10LB of Light Roast Coffee Beans",
            "price": 67.0,
            "isTaxable": true,
            "isArchived": false
        }
    },

这是我的Vuejs前端代码:

代码语言:javascript
复制
                    <template v-slot:[`item`]="{ inventory }">
                      <v-select
                        :items="inventory.product.name"
                        label="Product Receive"
                      ></v-select>
                    </template>

这是我正在使用的脚本(Typescript),这可能与它有关。

代码语言:javascript
复制
  inventory: IProductInventory[] = [];

  async intialize() {
    this.inventory = await inventoryService.getInventory();
    await this.$store.dispatch("assignSnapshots");
  }
  async created() {
    await this.intialize();
  }

这是IProductInventory和IProduct的接口

代码语言:javascript
复制
export interface IProduct {
  id: number;
  createdOn: Date;
  updatedOn: Date;
  name: string;
  description: string;
  price: number;
  isTaxable: boolean;
  isArchived: boolean;
}

export interface IProductInventory {
  id: number;
  product: IProduct;
  quantityOnHand: number;
  idealQuantity: number;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-12 16:45:19

您应该通过items prop传递数组:

代码语言:javascript
复制
<v-select
  label="Product Receive"
  :items="inventoryItems">
</v-select>

对于inventoryItems,您可以使用计算属性:

代码语言:javascript
复制
computed: {
  inventoryItems () {
    return yourArray?.map(inventory => ({
      text: inventory.product.name,
      value: inventory.product.id
    }))
  }
}

有关更多详细信息,请参阅v-select API

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67499817

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档