【发布时间】:2019-03-23 05:35:39
【问题描述】:
我创建了一个页面,我想通过 API 调用从数据库中获取我的所有数据,但我对 VueJS 和 Javascript 也有点陌生,我不知道我哪里弄错了。我确实使用 Postman 对其进行了测试,并且得到了正确的 JSON。
这是我得到的:
[__ob__: Observer]
length: 0
__ob__: Observer {value: Array(0), dep: Dep, vmCount: 0}
__proto__: Array
这就是我想要的:
(140) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, …]
[0 … 99]
[100 … 139]
length: 140
__ob__: Observer {value: Array(140), dep: Dep, vmCount: 0}
__proto__: Array
这是我的 Vue 模板文件:
<template>
<div>
<h2>Pigeons in the racing loft</h2>
<div class="card-content m-b-20" v-for="pigeon in pigeons" v-bind:key="pigeon.id">
<h3>{{ pigeon.id }}</h3>
</div>
</div>
</template>
<script>
export default {
data(){
return{
pigeons: [],
pigeon: {
id: '',
sex: '',
color_id: '',
pattern_id: '',
user_id: '',
loft_id: '',
country: '',
experience: '',
form: '',
fatique: ''
},
pigeon_id: ''
}
},
created(){
this.fetchPigeons();
console.log(this.pigeons); // Here I got the observer data instead my array
},
methods: {
fetchPigeons(){
fetch('api/racingloft')
.then(res => res.json())
.then(res => {
console.log(res.data); // Here I get what I need
this.pigeons = res.data;
})
}
}
}
</script>
我也试过用 axios 来做,但它给了我完全相同的东西。当我从它提供我的数据的方法中对其进行控制台时,但在外部它什么也没提供。
【问题讨论】:
-
尝试将创建函数中的console.log替换为
-
你试图在异步获取完成之前将数据记录在 created() 中,所以它仍然是空的(除了 Vue 用来检测数据变化的 Observable,例如当获取完成,因此它可以重新渲染组件。)
-
那么,问题出在哪里?今天发现了同样的错误
标签: javascript arrays laravel vue.js axios