0

我正在调用 API 并将数据存储到数组( users[] )中,我可以在控制台中看到 users[] 正在被填充,但data-table没有显示任何行。

<template>
<v-data-table
 :headers="headers"
 :items="users"
 class="elevation-1"
 :dark="true"
>

<template v-slot:item.deaths="{ item }">
  <v-chip :color="getColor(item.deaths)" dark>{{ item.deaths }}</v-chip>
</template>

<script>
 export default {

  data () {
   return {

   indiaTotalCase: null,
   indianDeaths: null,
   indianDischarged: null,

   headers: [
      { text: 'State', align: 'start', sortable: false, value: 'loc'},
      { text: 'Cases', value: 'totalConfirmed' },
      { text: 'Deaths', value: 'deaths' },
      { text: 'Discharged', value: 'discharged' } 
   ],
   users: [],
   }
  },

  mounted(){
     this.getdata();
     console.log("call finished");
  },

  methods: {
     getColor (deaths) {
      if (deaths > 100) return 'red'
      else if (deaths > 50) return 'orange'
      else return 'green'
     },

     getdata() {
        this.$http.get('https://api.rootnet.in/covid19-in/stats/latest')
          .then(response =>{
           return response.json();
           })

           .then(res =>{
             const data = res.data;
             this.indiaTotalCase = data.summary.total;
             this.indianDeaths = data.summary.deaths;
             this.indianDischarged = data.summary.discharged;
             const regionalData = data.regional;

           for(let index in regionalData){
             this.users[index] = regionalData[index];
           }

         });
        }
       },

</script>

我是 vue 新手,很抱歉代码格式错误。这是我得到的输出。图片

4

1 回答 1

0

请修改getdata方法如下,它应该工作

      getdata() {
  this.$http.get('https://api.rootnet.in/covid19-in/stats/latest')
  //  .then(response =>{
  //    return response.json();
  //  })
    .then(res =>{
       console.log('api res ',res)
      const data = res.body.data;
      this.indiaTotalCase = data.summary.total;
      this.indianDeaths = data.summary.deaths;
      this.indianDischarged = data.summary.discharged;
      const regionalData = data.regional;
      //  for(let index in regionalData){
      //    this.users[index] = regionalData[index];
      // }
      this.users = regionalData;
    });
于 2020-06-07T04:08:12.010 回答