我正在尝试将Kendo UI上的数据与来自Vuex getter的数据绑定。
我尝试过以下但没有运气。请帮助我在vuex或kendo上遗漏了什么。
Kendo Extensions:
<kendo-grid :data-source="kendoDataSource">
</kendo-grid>
组件:
computed: {
customers() {
return this.$store.getters.customers;
}
},
data() {
return {
kendoDataSource: {
schema: {
data: function(response) {
return response;
},
model: {
id: "CustomerID",
fields: {
CompanyName: { type: "string" },
}
}
},
transport: {
read: function(options) {
options.success(this.customers);
}
}
}
};
我收到错误。 TypeError: Cannot read property 'length' of undefined
当我尝试调试kendo传输中的this.customers
时,对象this.customers
始终为空。
数据的格式如下所示:
[
{
"CustomerID": "ALFKI",
"CompanyName": "Alfreds Futterkiste"
},
{
"CustomerID": "ANATR",
"CompanyName": "Ana Trujillo Emparedados y helados"
}
]
Store.js
export default {
state: {
customers: JSON.parse(JSON.stringify(customers))
},
getters: {
customers(state) {
return state.customers;
}
}
};
当我尝试直接在options.success(this.customers);
与下面显示的方式一样,网格成功填充了数据。但是当我尝试使用getters
进行绑定时,会出现错误。
TypeError: Cannot read property 'length' of undefined
options.success([
{
"CustomerID": "ALFKI",
"CompanyName": "Alfreds Futterkiste",
},
{
"CustomerID": "ANATR",
"CompanyName": "Ana Trujillo Emparedados y helados",
}
]);
答案 0 :(得分:1)
我认为你想使用计算属性而不是数据。
computed: {
customers() {
return this.$store.getters.customers;
},
kendoDataSource() {
const customers = this.customers
return {
schema: {
data: function(response) {
return response;
},
model: {
id: "CustomerID",
fields: {
CompanyName: { type: "string" },
}
}
},
transport: {
read: function(options) {
options.success(customers);
}
}
}
}
}
}