vue 實現todolist,包含添加,刪除,統計,清空,隱藏功能
添加:生成列表結構(v-for+數組)、獲取用戶輸入(v-model)、通過回車新增數據(v-on+.enter)
刪除:點擊刪除指定內容(v-on+splice索引)
統計:統計信息個數(v-text+length)
清空:點擊刪除所有信息(v-on)
隱藏:沒有數據時,隱藏元素(v-show/v-if+數組非空)
<template> <div id="app"> <div id="todolist"> <input type="text" v-model="inputVal" @keyup.enter="add" /> <ul> <li v-for="(value,index) in list"> <div> <span>{{index+1}}</span> <label>{{value}}</label> <button @click="del(index)">刪除</button> </div> </li> </ul> <!-- 隱藏 --> <footer v-show="list.length!=0"> <span> <!-- 統計 --> <em>{{list.length}}</em> list </span> <!-- 清空 --> <button @click="clear" >clear</button> </footer> </div> </div> </template> <script> export default { name: "App", data: () => { return { list: ["aaa", "bbb", "ccc"], inputVal: "" }; }, methods: { // 添加 add: function() { this.list.push(this.inputVal); }, // 刪除 del:function(index){ this.list.splice(index,1) }, // 清空 clear:function(){ this.list=[] } } }; </script> <style> #app { font-family: "Avenir", Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: #2c3e50; margin-top: 60px; } ul, li { list-style: none; } </style>