【发布时间】:2017-06-05 10:43:15
【问题描述】:
所以我尝试在 Vue JS 中使用以下组件:
Vue.component('careers', {
template: '<div>A custom component!</div>',
data: function() {
var careerData = [];
client.getEntries()
.then(function (entries) {
// log the title for all the entries that have it
entries.items.forEach(function (entry) {
if(entry.fields.jobTitle) {
careerData.push(entry);
}
})
});
return careerData;
}
});
以下代码会发出类似这样的错误:
[Vue warn]: data functions should return an object:
https://vuejs.org/v2/guide/components.html#data-Must-Be-a-Function
(found in component <careers>)
但是,如您所见,我正在通过我的所有 Contentful entries 运行 foreach,然后条目中的每个对象都被推送到数组中,然后我尝试返回数组,但出现错误。
知道如何将我的所有entries 提取到组件内的数据对象中吗?
当我在 Vue 组件之外使用 client.getEntries() 函数时,我得到以下数据:
【问题讨论】:
-
错误表示数据函数必须返回
Object,而不是Array?
标签: javascript vue.js vue-component