简体   繁体   English

如何访问变体中的颜色? 在 Vue.js 中

[英]How do I access the colors in the variant? in vue js

I want to loop the variant , and also want to get data from colors我想循环变体,还想从颜色中获取数据

i think my loop code was wrong我认为我的循环代码是错误的

<span v-for="{items, index} in product.variants" :key="index">
  <em v-for="(item, index) in items.colors" :key="index">{{ item.name }}</em>
</span>

this is my json这是我的json

"variants": [
    {
     "size": "39",
     "colors": [
        {
          "name": "Red",
          "stock": 2
        },
        {
        "name": "Black",
        "stock": 1
        }
      ]
    },
    {
     "size": "39",
     "colors": [
        {
          "name": "Red",
          "stock": 2
        },
        {
          "name": "Black",
          "stock": 1
        }
      ]
    }
  ]

can you help me how to do that?你能帮我怎么做吗?

Try this.尝试这个。

<span v-for="(item1, index1) in product.variants" :key="index1">
  <em v-for="(item2, index2) in item1.colors" :key="index2">{{ item2.name }}</em>
</span>

Where is your product?你的产品在哪里? and

Curly braces are wrong in v-for="{items, index} in product.variants" . v-for="{items, index} in product.variants"花括号是错误的。 It should be normal parentheses.它应该是正常的括号。

The following works:以下工作:

<template>
  <div>
    <span v-for="(items, index) in product.variants" :key="index">
    <em v-for="(item, index) in items.colors" :key="index">{{ item.name }}</em>
    </span>
  </div>
</template>

<script>
export default {
  data(){
    return {
      product: {
        variants: [
          {
            "size": "39",
            "colors": [
              {
                "name": "Red",
                "stock": 2
              },
              {
                "name": "Black",
                "stock": 1
              }
            ]
          },
          {
            "size": "39",
            "colors": [
              {
                "name": "Red",
                "stock": 2
              },
              {
                "name": "Black",
                "stock": 1
              }
            ]
          }
        ]
      }
    }
  }
}
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM