1、store.js(读取写入到localstorage)

const STORAGE_KEY="todos-vuejs"
export default{
fetch(){
return JSON.parse(window.localStorage.getItem(
STORAGE_KEY||"[]"))
},
save(items){
window.localStorage.setItem(STORAGE_KEY,JSON.stringify(items))
}
}

2、App.vue

<template>
<div id="app">
<h1>{{title}}</h1>
<h1 v-text="title"></h1>
<h1 v-html="title"></h1>
<input type="text" v-model="newItem" v-on:keyup.enter="addNew">
<ul>
<li v-for="item in items" v-bind:class="{finished:item.isFinished}" v-on:click="toggleFinish(item)">
{{item.label}}
</li>
</ul> </div>
</template> <script>
import Store from './Store'
//console.log(Store)
export default { data () {
return {
title: '<span>?</span>this is a todolist',
items:Store.fetch(),
// items:[
// {
// label:'coding',
// isFinished:false // },
// {
// label:'walking',
// isFinished:true // }
// ],
newItem:''
}
},
watch:{
items:{
handler:function(items){
// console.log(val,oldVal)
// console.log(items); Store.save(items);
// console.log(items); }
},
deep:true//也检测items内部的key的变化
},
methods:{
toggleFinish:function(item){
// console.log(item);
item.isFinished=!item.isFinished;
console.log(this.items);
},
addNew:function(){
// this.newItem;
// console.log(this.newItem);
this.items.push({
label:this.newItem,
isFinished:false
})
this.newItem='';
}
}
}
</script> <style>
.finished{
text-decoration:underline;
}
#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;
}
</style>

vue中使用localstorage的更多相关文章

  1. vue中使用localStorage存储信息

    一 什么是localStorage 对浏览器来说,使用 Web Storage 存储键值对比存储 Cookie 方式更直观,而且容量更大,它包含两种:localStorage 和 sessionSto ...

  2. vue中操作localstorage

    首先在子组件将localstorage方法进行封装 在父组件中对其进行引用 将输入的值存入到定义的searchHistory数组中,存储localstorage需要传两个参数,变量名为searchHi ...

  3. vue中 localStorage的使用方法(详解)

    vue中实现本地储存的方法:localStorage,在HTML5中,新加入了一个localStorage特性,这个特性主要是用来作为本地存储来使用的,解决了cookie存储空间不足的问题(cooki ...

  4. vue中Axios的封装和API接口的管理

    前端小白的声明: 这篇文章为转载:主要是为了方便自己查阅学习.如果对原博主造成侵犯,我会立即删除. 转载地址:点击查看 如图,面对一团糟代码的你~~~真的想说,What F~U~C~K!!! 回归正题 ...

  5. 学习笔记——在vue中如何配置Jest(一)

    最近在搞Jest单元测试,如何在vue中安装和使用jest我就不说了,前一篇文章简单的说了一下在使用jest时遇到的一些问题,但是我觉得并没有真正的解决的很好.后面会在学习过程中更新前面的那篇文章,加 ...

  6. vue中的路由独享守卫的理解

    1.vue中路由独享守卫意思就是对这个路由有一个单独的守卫,因为他的守卫方式于其他的凡是不太同 独享守卫于前置守卫使用方法大致是一样的 在路由配置的时候进行配置, { path:'/login', c ...

  7. 【翻译】使用Vuex解决Vue中的身份验证

    翻译原文链接:https://scotch.io/tutorials/handling-authentication-in-vue-using-vuex 我的翻译小站:https://www.zcfy ...

  8. vue中如何实现数据的双向绑定

    vue中如何实现数据的双向绑定 实现视图变化数据跟着变:分两步,上面get中的为第二步(即再次读取的时候会调用get方法得到之前设置的值,以此来实现动态改变) 由于直接写obj.name = this ...

  9. vue中遇到的坑

    如何解决在vue中替换图片.一个使用base64,而我们使用zepto之后,src找不到资源,因为已经打包了,难道强行写base64. 1. 引入文件时语法很重要! import "Foot ...

随机推荐

  1. 科技界、IT届的外号

    牙膏厂 = Intel     挤牙膏来形容缓慢的升级速度 农企(推土机,打桩机,压路机).阿曼达.按摩店  = AMD 两弹元勋.老黄 = 黄仁勋,    核弹.英伟达 = NVIDIA 大法 = ...

  2. 递归回溯groupSum

  3. STL中实现 iterator trail 的编程技巧

    STL中实现 iterator trail 的编程技巧 <泛型编程和 STL>笔记及思考. 这篇文章主要记录在 STL 中迭代器设计过程中出现的编程技巧,围绕的 STL 主题为 (迭代器特 ...

  4. VUE 打包后关于 -webkit-box-orient: vertical; 消失,导致多行溢出不管用问题

    VUE 打包后 -webkit-box-orient: vertical; 样式消失,导致页面样式爆炸,看了看解决方案,在这里总结一下: 实际上是 optimize-css-assets-webpac ...

  5. ubuntu10.4搭建eclipse for c++环境

    以下操作:经过验证,安装完成后,使用eclipse建立C++工程,直接编译就能运行了:说明:(1)不建议使用下载eclipse for java版本和CDT的方式来搭建环境,因为我没有安装成功:(2) ...

  6. python魔法方法-反射运算和增量运算

    反射运算 什么是反射运算符,其实就是反转了两个对象,下面先看一个普通运行符的实现: class Foo(object): def __init__(self, x): self.x = x def _ ...

  7. BZOJ.3809.Gty的二逼妹子序列(分块 莫队)

    题目链接 /* 25832 kb 26964 ms 莫队+树状数组:增加/删除/查询 都是O(logn)的,总时间复杂度O(m*sqrt(n)*logn),卡不过 莫队+分块:这样查询虽然变成了sqr ...

  8. 2、函数y=f(x)

    /* Note:Your choice is C IDE */ #include "stdio.h" /* 3.函数y=f(x)可表示为: */ void main() { int ...

  9. Struts2返回json数据xml中配置

    <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-/ ...

  10. memcached协议解析

    本文转载自:http://www.ccvita.com/306.html 协议memcached 的客户端使用TCP链接与服务器通讯.(UDP接口也同样有效,参考后文的 “UDP协议” )一个运行中的 ...