vue没有运行实例中保存的变量

时间:2019-03-01 05:39:25

标签: vuejs2

我是Vue js的新手,正在使用Codesandbox上的文档中的示例。这段代码向我显示错误

[Vue warn]: Property or method "todos" 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. See: https://vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.

我无法确切检测出问题出在哪里。

<template>
  <div id="app">
    <ol>
      <li v-bind:key="todo.text" v-for="todo in todos">{{ todo.text }}</li>
    </ol>
  </div>
</template>

<script>
import Vue from "vue";

var app = new Vue({
  el: "#app",
  data: {
    todos: [
      { text: "Learn JavaScript" },
      { text: "Learn Vue" },
      { text: "Build something awesome" }
    ]
  }
});
</script>

<style>
style here
</style>

这是代码和问题的live example

1 个答案:

答案 0 :(得分:1)

这是更新的代码。 您声明的todos变量不在vue实例中。

您应按以下方式声明数据属性。

    <template>
  <div id="app">
    <ol>
      <li v-bind:key="todo.text" v-for="todo in todos">{{ todo.text }}</li>
    </ol>
  </div>
</template>

<script>
import HelloWorld from "./components/HelloWorld";
export default {
  name: "App",
  components: {
    HelloWorld
  },
  data() {
    return {
      todos: [
        { text: "Learn JavaScript" },
        { text: "Learn Vue" },
        { text: "Build something awesome" }
      ]
    };
  }
};

https://codesandbox.io/s/9o2036km1r