<template>
<div id="app">
<input type="text" v-model="todo" v-on:keydown="addObj2($event)">
<button @click="addObj()">+添加</button>
<ul>
<h3>已完成</h3>
<li v-for="(item,key) in todoList" v-if="item.isdo"><input type="checkbox" v-model="item.isdo">{{item.todo}} <button @click="deleteObj(key)">刪除</button></li>
</ul>
<ul>
<h3>未完成</h3>
<li v-for="(item,key) in todoList" v-if="!item.isdo" v-bind:class="{'Done':!item.isdo}"><input type="checkbox" v-model="item.isdo">{{item.todo}} <button @click="deleteObj(key)">刪除</button></li>
</ul>
<router-view/>
</div>
</template>
<script>
export default {
name: 'App',
data (){
return{
todo:'',
todoList:[],
isdo:true,
}
},
methods:{//用于放置自定義方法
addObj(){
if(!Object.is(this.todo,'')){
let obj={};
[obj.todo,obj.isdo]=[this.todo,this.isdo];
this.todoList.push(obj);
}
this.todo='';
},addObj2(e){
if(e.keyCode===13){
let obj={};
[obj.todo,obj.isdo]=[this.todo,this.isdo];
this.todoList.push(obj);
this.todo='';
}
}
,deleteObj(key){
this.todoList.splice(key,1);//splice() 方法向/從數(shù)組中添加/刪除項目,然后返回被刪除的項目。
//arrayObject.splice(index,howmany,item1,.....,itemX):index 必需。整數(shù),規(guī)定添加/刪除項目的位置亩进,使用負(fù)數(shù)可從數(shù)組結(jié)尾處規(guī)定位置。howmany 必需缩歪。要刪除的項目數(shù)量归薛。如果設(shè)置為 0,則不會刪除項目匪蝙。item1, ..., itemX 可選苟翻。向數(shù)組添加的新項目。
}
}
}
</script>
<style>
app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
color: #2c3e50;
margin-top: 60px;
}
.Done{
background: #ccc;
}
</style>