我的部件vue.js是这样的:如何解决“Uncaught(在承诺)TypeError:response.body.forEach不是一个函数”? (vue.js 2)
<script>
export default{
name: 'CategoryBsSelect',
template: '\
<select class="form-control" v-model="selected" required>\
<option v-for="option in options" v-bind:value="option.id" v-bind:disabled="option.disabled">{{ option.name }}</option>\
</select>',
//props: {list: {type: String, default: ''}},
mounted() {
this.fetchList();
},
data() {
return {
selected: '',
options: [{id: '', name: 'Select Category'}]
};
},
methods: {
fetchList: function() {
this.$http.post(window.BaseUrl+'/member/category/list').then(function (response) {
//this.$set('list', response.data);
console.log(JSON.stringify(response.body))
response.body.forEach(function(item){
this.$set(this.options, item.id, item);
}, this);
});
},
}
};
</script>
console.log(JSON.stringify(response.body))
的结果是:
{"20":"Category 1","21":"Category 2","22":"Category 3"}
欲显示在选择的值的响应。但执行时,在控制台存在这样的错误:
Uncaught (in promise) TypeError: response.body.forEach is not a function at VueComponent.
有没有人可以帮助我?
对象没有一个'forEach' - 使用'Object.keys' –
你有什么期望'item'和'item.id'是? – Bergi