1、带localstorage的
https://files.cnblogs.com/files/weibo258/vuedemo02%E5%B0%81%E8%A3%85localstorage.zip
<template>
<div id="app">
<input type="text" v-model="todo" @keydown="doAdd($event)">
<button @click="doAdd()">+增加</button>
<br>
<hr>
<br>
<h2>已进行中</h2>
<ul>
<li v-for="(item,key) in list" v-if="!item.checked" >
<input type="checkbox" v-model="item.checked" @change="saveList()"/> {{item.title}}---- <button @click="removeData(key)">删除</button>
</li>
</ul>
<br>
<h2>已完成</h2>
<ul class="finish">
<li v-for="(item,key) in list" v-if="item.checked">
<input type="checkbox" v-model="item.checked" @change="saveList()"/> {{item.title}}---- <button @click="removeData(key)">删除</button>
</li>
</ul>
<button @click="getList()">获取list的值</button>
</div>
</template>
<script>
export default {
data(){
return{
todo:'',
list:[
// {
// title:'录制notejs',
// checked:true
// },{
// title:'录制ionic',
// checked:false
// }
]
}
}
,methods:{
doAdd(e){
//1.获取文本框输入的值 2.把文本框的值push到list里面
if(e.keyCode==13){
this.list.push(
{
title:this.todo,
checked:false
}
)
}
localStorage.setItem('list',JSON.stringify(this.list))
// this.todo='';
},
removeData(key){
// alert(key)
this.list.splice(key,1);
localStorage.setItem('list',JSON.stringify(this.list));
},saveList(){
localStorage.setItem('list',JSON.stringify(this.list));
},getList(){
console.log(this.list)
}
},mounted() {/*生命周期函数*/
var list=JSON.parse(localStorage.getItem('list'));
if(list){/*注意判断*/
this.list=list
}
}
}
</script>
<style scoped>
.finish {
background: #eee;
}
</style>
2、封装使用localstorage的
下载地址:https://files.cnblogs.com/files/weibo258/vuedemo02%E5%B8%A6localstorage.zip
首先创建一个src/model/storage.js
内容为:
//封装操作localstorage本地存储的方法 模块化的文件
//nodejs 基础
var storage={
set(key,value){
localStorage.setItem(key,JSON.stringify(value))
},
get(key){
return JSON.parse(localStorage.getItem(key));
},
remove(key){
localStorage.removeItem(key);
}
}
export default storage;
App.vue:
<template>
<div id="app">
<input type="text" v-model="todo" @keydown="doAdd($event)">
<button @click="doAdd()">+增加</button>
<br>
<hr>
<br>
<h2>已进行中</h2>
<ul>
<li v-for="(item,key) in list" v-if="!item.checked" >
<input type="checkbox" v-model="item.checked" @change="saveList()"/> {{item.title}}---- <button @click="removeData(key)">删除</button>
</li>
</ul>
<br>
<h2>已完成</h2>
<ul class="finish">
<li v-for="(item,key) in list" v-if="item.checked">
<input type="checkbox" v-model="item.checked" @change="saveList()"/> {{item.title}}---- <button @click="removeData(key)">删除</button>
</li>
</ul>
<button @click="getList()">获取list的值</button>
</div>
</template>
<script>
import storage from './model/storage.js'
// 相当于 var storage={
// ......
// }
console.log(storage)
export default {
data(){
return{
todo:'',
list:[
// {
// title:'录制notejs',
// checked:true
// },{
// title:'录制ionic',
// checked:false
// }
]
}
}
,methods:{
doAdd(e){
//1.获取文本框输入的值 2.把文本框的值push到list里面
if(e.keyCode==13){
this.list.push(
{
title:this.todo,
checked:false
}
)
}
storage.set('list',this.list)
this.todo='';
},
removeData(key){
// alert(key)
this.list.splice(key,1);
storage.set('list',this.list)
},
saveList(){
storage.set('list',this.list)
},getList(){
console.log(this.list)
}
},mounted() {/*生命周期函数*/
var list=storage.get('list',this.list);
if(list){/*注意判断*/
this.list=list
}
}
}
</script>
<style scoped>
.finish {
background: #eee;
}
</style>