如何在vuejs中循环

tquggr8v  于 2023-04-07  发布在  Vue.js
关注(0)|答案(3)|浏览(118)

这是我的剧本

<script>
import axios from "axios";
export default {
  data() {
    return {
      responseObject: {}
    };
  },
  async mounted() {
    try {
      let response = await axios.get("http://localhost:4000/api/blogs");
    this.responseObject = response.data;
    } catch (e) { 
    }
  },
};
</script>

这是我的responseObject json

responseObject: {
        resource: "services",
        status: "success",
        data: [
          {
            service_id: "0001",
            service_name: "Buy Airtime",
            service_type: "airtime",
          },
 {
            service_id: "0002",
            service_name: "Buy Data",
            service_type: "Data",
          },
        ],
      },

这是我的模板,我使用{{service}}获取整个JSON

<ul>
      <li v-for="service in  responseObject" :key="service.service_id">
        {{ service }}
      </li>
    </ul>

请问我怎么能循环通过数据和得到他们中的任何一个

service_name: "Buy Airtime",
service_type: "airtime",

我试过了

{{servive.service_name}}

但它不返回任何内容

91zkwejq

91zkwejq1#

下面是如何只重复数据。

<ul>
      <li v-for="service in responseObject.data" :key="service.service_id">
        {{ service.service_name }} :: {{ service.service_type }}
      </li>
    </ul>
fnatzsnv

fnatzsnv2#

你应该试试这个。

<ul>
    <li v-for="service in responseObject.data">
        {{ service.service_name }}
    </li>
</ul>
<!-- OR -->
<ol>
    <li v-for="service in responseObject.data">
        {{ service.service_type }}
    </li>
</ol>

还有,我不知道你为什么还拿不到数据,我这里已经有了。

5ktev3wc

5ktev3wc3#

你可以通过在你的v-for语句中添加点符号来访问子“data”数组(“responseObject.data“)-

<ul>
      <li v-for="service in  responseObject.data"    :key="service.service_id">
        {{ service.service_name }}
      </li>
    </ul>

Codepen - https://codepen.io/bsod_/pen/wvYwBrj

相关问题