寫Vue一陣子了,但蠻少發文的所以來介紹一點小實作,經典又簡單的todolist,有輸入、刪除、編輯、已完成的功能。 主要學習點: V-for的使用。 資料的雙向、單向綁定。 V-if的簡單使用。 事件觸發的綁定。 那就先來做個樣板吧!! <template> <div id="app"> <input/> <ul> <li v-for="(item,index) in items" :key="index" :class="{finished:item.isFinished}"> <span>{{item.label}}</span> </ul> </div> </template> <script> export default { data () { return { items: [ { label: 'read books', isFinished: false }, { label: 'eat dinner', isFinished: true } ], } } } </script> <style> .finished { text-decoration: line-through; } </style> 這樣的樣子會呈現這樣,單純的輸入與一開始的內容。 透過V-for來列出我們物件裡面的資料,並透過V-bind綁定class並與style配合劃掉已完成的事項,非常簡單就做出單純的layout。接下來我們要輸入文字並添加事項囉! <input class="addtext" @keyup.enter=&