Vue属性定义警告,即使它是在实例上定义的

问题描述 投票:2回答:1

编辑 - 我已经在github上设置了一个带有错误代码的repo,如果有人想把它拉下来并自己看错误: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数据对象上定义属性的结果。不幸的是,在这种情况下,我确实添加了columnsitemsto新的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>
javascript vue.js vuejs2 kendo-grid vue-cli-3
1个回答
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>
© www.soinside.com 2019 - 2024. All rights reserved.