我正在使用VueJS并收到一条我之前没有过的错误消息。事件var是一个数组,所以它应该可以工作吗?
在我的HTML文件中,我包括:
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.10/vue.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue-resource/1.2.0/vue-resource.js"></script>
当我记录response.data的输出时,我看到一个数组:
未捕获(承诺)TypeError:response.data.map不是函数 在Vue 3美元。 (main.js:17)
var app = new Vue({
el: '#app',
data: {
responders: [],
incidents: []
},
mounted: function () {
this.getIncidents();
},
methods: {
getIncidents: function() {
console.log('getIncidents');
var app = this;
this.$http.get('/api/v1/incidents').then(function(response) {
// set data on vm
var incidentsReceived = response.data.map(function (incident) {
return incident
});
Vue.set(app, 'incidents', incidentsReceived);
});
}
})
答案 0 :(得分:1)
您无法映射响应。尝试将响应数据设置为您的数据,
this.$http.get('/api/v1/incidents').then((response) => {
// set data in your data object
this.incidents = response.data
// Now you can map over the incidents and return
return this.incidents.map(incident => return)
});