vue--$index is not defined报错

今天学习Vue中遇到了一个报错信息:$index is not defined,是我写了个for循环在HTML中,然后是因为版本的问题

下面是解决方法:

原来的是 v-for="todo  in  todos"    


v-on:click="removeTodo($index)//这个仅仅适用于1.0版本-------------


这个在Vue1.0版本中式适用的可以直接使用$index,但是在2.0是不适合的

在Vue 2.0版本中获取索引我们需要通过 v-for = "(person ,index)  in items ",  点击事件不能使用$index,应该使用 v-on:click="removeTodo(index)

  • {{todo.text}}
var exe = new Vue({
            el: ".app",
            data: {
                newTodo: '',
                todos: [
                    { text: 'learn' },
                    // {text:'vue'},
                    // {text:'buildlk'}
                ]
            },
            methods: {
               addTodo: function () {
                    var text = this.newTodo.trim();
                    if (text) {
                        this.todos.push({ text: text });
                        this.newTodo = '';
                    }
                },
                removeTodo: function (index) {
                    this.todos.splice(index, 1);
                }
            }
        });

 

你可能感兴趣的:(实习所学)