Vuex 如何在传输阅读中将商店绑定到 KendoUi-Vue Grid

Vuex How to bind store to KendoUi-Vue Grid in transport read

我正在尝试将 Kendo UI 上的数据与来自 Vuex getter 的数据绑定。

我尝试了以下但没有成功。请帮助我是否在 vuex 或 kendo.

上遗漏了什么

Kendo 扩展名:

<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",
        }
    ]);

我认为您想使用计算属性而不是数据。

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);
          }
        }
      }
    }
  }
}