即使在实例上定义了Vue属性定义警告

时间:2019-03-07 13:49:41

标签: javascript vue.js vuejs2 kendo-grid vue-cli-3

编辑-我已经在github上安装了一个带有错误代码的仓库,如果有人想将其删除并自己查看错误:https://github.com/andrewjrhill/what-the-instance-grid。您可以运行npm run serve来启动网络服务器。


我遇到一个问题,我的Vue抛出以下错误:

[Vue warn]: Property or method "columns" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property.
[Vue warn]: Property or method "items" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property.

这是Vue应用程序中一个非常普遍的问题,通常是由于未在Vue数据对象上定义属性而导致的。在这种情况下,我确实很遗憾地将columnsitems添加到了新的Vue调用中。任何想法为什么我会收到此错误?看来模板根本没有数据。

该项目是由最新的Vue-CLI生成的,如果有任何区别,它将在runtimeCompiler: true文件中使用vue.config.js标志。

有问题的.vue文件:

<template>
  <div id="vueapp" class="vue-app">
    <Grid :columns="columns" :data-items="items" :style="{ height: '280px' }"></Grid>
  </div>
</template>

<script>
import Vue from "vue";

import { Grid } from "@progress/kendo-vue-grid";

Vue.component("Grid", Grid);

new Vue({
  el: "#vueapp",
  data: function() {
    return {
      items: [],
      columns: [
        { field: "ProductID" },
        { field: "ProductName", title: "Product Name" },
        { field: "UnitPrice", title: "Unit Price" }
      ]
    };
  },
  methods: {
    createRandomData(count) {
      const productNames = [
        "Chai",
        "Chang",
        "Syrup",
        "Apple",
        "Orange",
        "Banana",
        "Lemon",
        "Pineapple",
        "Tea",
        "Milk"
      ];
      const unitPrices = [12.5, 10.1, 5.3, 7, 22.53, 16.22, 20, 50, 100, 120];

      return Array(count)
        .fill({})
        .map((_, idx) => ({
          ProductID: idx + 1,
          ProductName:
            productNames[Math.floor(Math.random() * productNames.length)],
          UnitPrice: unitPrices[Math.floor(Math.random() * unitPrices.length)]
        }));
    }
  },
  mounted() {
    this.items = this.createRandomData(50);
  }
});

export default {
  name: "App",
  components: {
    Grid
  }
};
</script>

1 个答案:

答案 0 :(得分:3)

请勿在App.vue组件中重新实例化Vue。
像这样修复(您存储库中的文件):

main.js:

import App from './App.vue'
import Vue from 'vue'
import { Grid } from "@progress/kendo-vue-grid";

Vue.component("Grid", Grid);

Vue.config.productionTip = false

new Vue({
  render: h => h(App),
}).$mount('#vueapp')

App.vue:

<template>
  <div id="vueapp" class="vue-app">
    <Grid :columns="columns" :data-items="items" :style="{ height: '280px' }"></Grid>
  </div>
</template>

<script>
  export default {
    name: "App",
    data: function() {
      return {
        items: [],
        columns: [
          { field: "ProductID" },
          { field: "ProductName", title: "Product Name" },
          { field: "UnitPrice", title: "Unit Price" }
        ]
      };
    },
    methods: {
      createRandomData(count) {
        // your code
      }
    },
    mounted() {
      this.items = this.createRandomData(50);
    }
  };
</script>