1 App.vue
<template>
<div id="app">
<input type="text" v-model="thing" @keydown="keyhandle($event)"/><button @click="add()">新增</button>
<h2>待办列表</h2>
<ul>
<li v-for="(item,key) in list" v-if="!item.status">
<input type="checkbox" v-model="item.status" @change="save()"/>{{item.name}}
<button @click="del(key)">删除</button>
</li>
</ul>
<br>
<hr>
<br>
<h2>已完成列表</h2>
<ul>
<li v-for="(item,key) in list" v-if="item.status">
<input type="checkbox" v-model="item.status" @change="save()"/>{{item.name}}
<button @click="del(key)">删除</button>
</li>
</ul>
</div>
</template>
<script>
import store from "./store.js";
export default {
name: "app",
data() {
return {
thing: "thing1",
list:[]
}
},
methods:{
add(){
var a = {"name":this.thing,"status":false}
this.list.push(a);
store.set("list",this.list);
this.thing="";
},
del(k){
this.list.splice(k,1);
store.set("list",this.list);
},
keyhandle(e){
//console.log(e.keyCode);
if(e.keyCode==13){
this.add();
}
},
save(){
store.set("list",this.list);
}
},
mounted(){
if(store.get("list")){
this.list = store.get("list");
console.log(this.list);
}
},
};
</script>
<style>
</style>
2 store.js
var store={
set(key,value){
localStorage.setItem(key,JSON.stringify(value));
},
get(key){
return JSON.parse(localStorage.getItem(key));
},
remove(key){
localStorage.removeItem(key);
}
}
export default store;