简体   繁体   English

在 VUEJS 中打印 JSON 的属性

[英]Print attributes of a JSON in VUEJS

I would like to read JSON data in my VueJS code.我想在我的 VueJS 代码中读取 JSON 数据。

Here my code :这是我的代码:

<template>
 {{info}}
</template>


<script>
import axios from 'axios';

export default {
  data() {
    return {
      info: null
    };
  },
  mounted(){
    axios
      .get('data/products.json')
      .then(response => (this.info = response.data.data)),
  }
</script>

I have that on the website :我在网站上有:

[ { "id": "1000", "code": "1", "name": "Certificats", "description": "cert Description", "status": "ERROR"}, { "id": "1005", "code": "2", "name": "Autre", "description": "Product Description", "status": "ERROR"} ] [ { "id": "1000", "code": "1", "name": "Certificats", "description": "cert Description", "status": "ERROR"}, { "id": " 1005", "code": "2", "name": "Autre", "description": "Product Description", "status": "ERROR"}]

I would like to be able to call only for example info.id to print just the id or to use it like an attribute in a v-if or in a v-for.我希望能够仅调用例如 info.id 来仅打印 id 或将其用作 v-if 或 v-for 中的属性。

For example be able to do something like that :例如能够做这样的事情:

 <div :v-for="number in info.id">
   {{number}}
 </div>

Do you know how to do it ?你知道怎么做吗?

Thanks谢谢

I'd suggest you change your v-for loop, and reference whichever key you want to access我建议你改变你的 v-for 循环,并引用你想要访问的任何键

<div :v-for="item in info">
   {{ item.id }}
    {{ item.name }}
</div>

and so on.等等。

 <div v-for="infoItem in info" :key="infoItem.id">
    {{ infoItem.id }}
 </div>

Another solution to show only the ids of the errors would be仅显示错误 ID 的另一种解决方案是

    <div v-for="infoItem in info" :key="infoItem.id">
      <div v-if="infoItem.status === 'ERROR'"
        {{ infoItem.id }}
      </div>
    </div>

It's bad practise to combine v-for and v-if in the same line.在同一行中组合 v-for 和 v-if 是不好的做法。

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

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