这是我的脚本,它调用axios并以帖子形式获取数据
<script>
import axios from 'axios'
export default {
name: 'App',
mounted: function () {
axios.get('API URL')
.then(response => this.posts = response.data)
},
data() {
return {
posts: null
}
},
};
</script>
我在视图上的代码试图从上面的脚本中获取数据作为帖子
<template>
<div id="app">
<ul>
<li v-for="post in posts" v-text="post.createdAt"></li>
</ul>
<div>
</template>
从API URL提取的SAMPLE数据如下所示
我能够以数组的形式在控制台日志中获取API DATA,但是当我从createdAT的数组中调用一个对象时,v-text = "post.createdAt"
不会打印/获取createdAt日期列表的列表。
刚刚解决了此文档,并使用了AXIOS TO CONSUME API,这是该https://vuejs.org/v2/cookbook/using-axios-to-consume-apis.html的链接。
以上我发布的代码效果很好。问题是我的API URL嵌套在data [data [object]]中。所以我从那个API调用数据的方式
由此
mounted: function () {
axios.get('API URL')
.then(response => this.posts = response.data)
}
对此
mounted: function () {
axios.get('API URL')
.then(response => this.posts = response.data.data)
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句