【问题标题】:Why am I able to display an entire JSON object, but not get a value from inside it?为什么我可以显示整个 JSON 对象,但不能从其中获取值?
【发布时间】:2020-07-03 04:58:12
【问题描述】:

我有 Vue 单文件组件,它显示产品的详细信息(无论如何它都意味着)。我所有的数据似乎都可以从 Vuex 商店加载。这是我的Product.vue 页面:

<template>
   <div class="page-content">
      {{Product}}               <!-- this works and shows JSON on the page -->
      {{Product.ProductTitle}}  <!-- this shows nothing at all -->
   </div>
</template>
<script>
    import {mapGetters} from 'vuex';
    export default {
        name: "Product",
        computed:
            {
                ...mapGetters({
                    Product: 'getProduct',
                })
            },
        serverPrefetch() {
            return this.getProduct();
        },
        mounted() {
            if (!this.Product.length) {
                this.getProduct();
            }
        },
        methods: {
            getProduct() {
                return this.$store.dispatch('loadProduct', {ProductID: this.$route.params.ProductID})
            }
        }
    }
</script>

计算出的Product 变量中的数据就是这样的:

[ { "ProductID": 12552, "ProductTypeID": 1, "ProductStatusID": 3, "ProductTitle": "Sony PlayStation 4 Pro" }]

我无法理解为什么{{Product}} 会显示整个 JSON 对象,而 {{Product.ProductTitle}} 却什么也没有显示?

更新:JSFiddle showing problem

【问题讨论】:

  • 在您的小提琴示例中,product 变量是一个列表列表。如果你这样做 {{Product[0][0].ProductTitle}} 它可以工作。
  • @drec4s 列表列表是什么意思?好像是从数据库里出来的样子
  • 变量声明后有双括号。

标签: json vue.js vuejs2 vue-component


【解决方案1】:

您在数组内部有一个对象,例如:[[{}]],因此您必须像这样获取数据:{{Product[0][0].ProductTitle}}

new Vue({
  el: "#app",
  data: {
    Product: [
      [{
        "ProductID": 14896,
        "ProductStatusID": 3,
        "CountryID": 207,
        "ProductTitle": "PS4 Pro Console +  Call of Duty: Modern Warfare / Death Stranding + Metro Exodus",
        "ProductItemDescription": "<p>The best PS4 Pro bundle offer!</p>"
      }]
    ]
  }
})
body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
}

#app {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  transition: all 0.2s;
}

li {
  margin: 8px 0;
}

h1 {
  color: black
}

h2 {
  color: blue;
  margin-bottom: 15px;
}

del {
  color: rgba(0, 0, 0, 0.3);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <article role="article" class="">
    <h1>
      {{Product[0][0].ProductTitle}}
      <!-- this doesn't work? -->
    </h1>
    <h2>
      {{Product}}
    </h2>
  </article>
</div>

【讨论】:

  • 不,我得到一个错误然后说"TypeError: Cannot read property 'ProductTitle' of undefined"
  • 这可能是因为 Vue 在加载产品数组之前尝试渲染它。首先检查 Product[0] 是否存在,例如{{Product[0] &amp;&amp; Product[0].ProductTitle}}
  • 如果我这样做 {{Product[0]}} 它工作正常并显示 JSON。如果我这样做{{Product[0].ProductTitle}},那么我会收到错误"TypeError: Cannot read property 'ProductTitle' of undefined"
  • 这很奇怪,你能在这里发布更多代码吗?创建sn -p 可能是
  • JSFiddle 完成显示问题jsfiddle.net/zvfo1n7a
猜你喜欢
  • 1970-01-01
  • 2013-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-14
相关资源
最近更新 更多