HTML代碼:
<div id = "app">
<input v-model="newTodo" v-on:keyup.enter="addTodo">
<ul>
<li v-for="todo in todos_1">
<span>{{ todo.text }}</span>
<button v-on:click="removeTodo($index)">X</button>
</li>
</ul>
</div>
JavaScript代碼:
new Vue({
el: '#app',
data: {
todos_1: [{
text: 'Learn JavaScript'
}]
},
methods: {
addTodo: function() {
var text = this.newTodo.trim()
if(text) {
this.todos_1.push({
text: text
})
this.newTodo = ''
}
},
removeTodo: function(index) {
this.todos_1.splice(index, 1)
}
},
})
運(yùn)行上面的代碼兜看,打開谷歌控制臺(tái),在輸入框輸入,發(fā)現(xiàn)控制臺(tái)報(bào)錯(cuò)眯杏,錯(cuò)誤原因:
[Vue warn]: You are setting a non-existent >path "newTodo" on a vm instance. Consider pre-initializing the property with the "data" option for more reliable reactivity and >better performance.。輸入框的例子在官方前面的例子也有出現(xiàn)壳澳,對(duì)比后發(fā)現(xiàn)v-model="newTodo"岂贩,中的“newTodo”沒有在>data中進(jìn)行定義,所以只需在data{}中加入newTodo: ' '即可巷波!這個(gè)錯(cuò)誤看起來好像在運(yùn)行過程中沒有什么影響萎津,但天知道以后會(huì)>不會(huì)被他坑慘,所以記錄一下抹镊!