【问题标题】:nuxt.js fetching data to vuetify tablenuxt.js 获取数据到 vuetify 表
【发布时间】:2021-01-26 14:20:42
【问题描述】:

我在获取 vuetify 表中的数据时遇到问题,它没有在边表中显示任何数据。

我的 Laravel API

Route::get('/businesslist', 'BusinessController@userlist')->name('businesslist');

Laravel API 控制器

 public function businesslist() {
      $businesslist = Business::paginate(2)->toJson(JSON_PRETTY_PRINT);
      return response($businesslist);
    }
}

Nuxt 代码

<template>
  <v-card>
    <v-card-title>
      Nutrition
      <v-spacer></v-spacer>
      <v-text-field
        v-model="search"
        append-icon="mdi-magnify"
        label="Search"
        single-line
        hide-details
      ></v-text-field>
    </v-card-title>
    <v-data-table
      :headers="headers"
      :items="registerlist"
      :search="search"
    ></v-data-table>
  </v-card>
</template>

<script>
  export default {
    data () {
      return {
        search: '',
        headers: [
    
          { text: 'SL,No', value: '' },
          { text: 'Name', value: 'name' },
          { text: 'Mobile  ', value: 'mobile_number' },
          { text: 'Location  ', value: 'location' },
          { text: 'Join Date  ', value: 'registration_date' },
          { text: 'Renewal Date  ', value: 'registration_renewal_date' },
        ],
        registerlist: [
          
        ],
}
              axios.get('/Businessregisterlist')
.then(res => this.registerlist = res.data.registerlist)
   

    },
  }
</script>

【问题讨论】:

  • .then(function(res){})中使用函数,因为this在ES6语法中可以表示全局对象。
  • @Mr.完美主义者能不能把你的答案贴出来让击球手理解
  • 好的。给出这个作为答案。

标签: vue.js vuejs2 axios vue-component vuetify.js


【解决方案1】:

首先,您必须在这样的函数中调用API

<script>
export default {
  data() {
    return {
       list: []
    }
  },
  methods: {
      callAPI() {
         axios.get('/', then(function(res){
            this.list = res.data.registerList;
         }))
      }
  },
   
  mounted(){
     this.callAPI(); // if you need that List on load then you can use mounted() otherwise that function will call during event.
  }
}
</script>

这里,在then() 里面,我认为最好使用function()。因为如果您使用函数,那么它将指示此对象。因此,我们可以轻松访问所有对象属性(例如:data()、methods() 等)。

【讨论】:

  • 我在我的代码中实现了你的代码,没有错误但数据不显示,
  • 你的控制台有数据吗?
  • then(function(res){ console.log(res.data.registerlist);}))
  • 完美主义者我在控制台中收到此错误,属性或方法“registerList”未在实例上定义,但在渲染期间被引用。
  • 这个错误告诉我们没有registerList。所以,console.log(res.data)。有那个名字的列表吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-25
  • 2021-06-25
  • 2020-09-10
  • 2020-11-07
  • 2021-09-02
  • 2019-07-15
  • 2020-07-25
相关资源
最近更新 更多