Vue.js - 未捕获的TypeError:无法读取属性&#39; push&#39;未定义的<匿名>:1:12

时间:2018-05-27 14:17:48

标签: javascript html5 google-chrome vue.js

我收到错误消息:

  

未捕获的TypeError:无法读取属性&#39; push&#39;未定义的   :1:12

尝试通过Chrome中的控制台推送另一个项目来列出待办事项:

app3.todos.push({ text: "New item" })

以下是我的代码:

HTML

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Web for Vue.Js</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script>
        window.onload = function () {
            var app3 = new Vue({
                el:"#app3",
                data: {
                    todos:[
                        {text: "1"},
                        {text: "2"},
                        {text: "3"},
                        {text: "4"}
                    ]
                }
            });
        }
    </script>
</head>
<body>
    <div id="app3">
        <ol>
            <li v-for="todo in todos">
                {{ todo.text }}
            </li>
        </ol>
    </div>
</body>

</html>

1 个答案:

答案 0 :(得分:1)

将您的代码移出页面加载的事件处理程序,一切都会好的。否则,您将无法通过控制台访问变量实例。

Budulinek昨天告诉过你。 :)

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Web for Vue.Js</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
    <div id="app3">
        <ol>
            <li v-for="todo in todos">
                {{ todo.text }}
            </li>
        </ol>
    </div>
    <script>
            var app3 = new Vue({
                el:"#app3",
                data: {
                    todos:[
                        {text: "1"},
                        {text: "2"},
                        {text: "3"},
                        {text: "4"}
                    ]
                }
            });
    </script>
</body>

</html>