听写2
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<div id="app">
<div>
ID: <input type="text" v-model="id">
<br>
姓名: <input type="text" v-model="name">
<br>
性别: <input type="text" v-model="id">
<button type="button" @click="add()">添加</button>
</div>
<table>
<thead>
<tr>
<th>ID:</th>
<th>姓名:</th>
<th>性别:</th>
<th>生日:</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr v-for= "person in people">
<td>{{person.id}}</td>
<td>{{person.name}}</td>
<td>{{person.sex}}</td>
<td>{{person.birthday|formatDate}}</td>
<td><button @click=del(person.id) >删除当前用户</button></td>
</tr>
</tbody>
</table>
</div>
<script src="js/vue.js"></script>
<script>
Vue.filter('formatDate', data => {
if (data == null || data == '') {
return '';
}
var date = new Date(data);
var dateStr = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
return dateStr;
});
var vm = new Vue({
el:'#app',
created() {
var people = JSON.parse(localStorage.getItem('people')||'[]')
this.people = people;
},
data: {
id:null,
name:null,
sex:null,
people:[
]
},
methods: {
add() {
var person = {
id:this.id,
name:this.name,
sex:this.sex,
birthday:new Date()
}
this.people.push(person);
localStorage.setItem("people",JSON.stringify(this.people));
},
del(id) {
this.people.some((person,i) =>{
if(person.id == id) {
this.people.splice(i,1);
localStorage.setItem("people",JSON.stringify(this.people));
return true;
}
})
}
}
})
</script>
</body>
</html>