vue中使用localstorage
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的更多相关文章
- vue中使用localStorage存储信息
一 什么是localStorage 对浏览器来说,使用 Web Storage 存储键值对比存储 Cookie 方式更直观,而且容量更大,它包含两种:localStorage 和 sessionSto ...
- vue中操作localstorage
首先在子组件将localstorage方法进行封装 在父组件中对其进行引用 将输入的值存入到定义的searchHistory数组中,存储localstorage需要传两个参数,变量名为searchHi ...
- vue中 localStorage的使用方法(详解)
vue中实现本地储存的方法:localStorage,在HTML5中,新加入了一个localStorage特性,这个特性主要是用来作为本地存储来使用的,解决了cookie存储空间不足的问题(cooki ...
- vue中Axios的封装和API接口的管理
前端小白的声明: 这篇文章为转载:主要是为了方便自己查阅学习.如果对原博主造成侵犯,我会立即删除. 转载地址:点击查看 如图,面对一团糟代码的你~~~真的想说,What F~U~C~K!!! 回归正题 ...
- 学习笔记——在vue中如何配置Jest(一)
最近在搞Jest单元测试,如何在vue中安装和使用jest我就不说了,前一篇文章简单的说了一下在使用jest时遇到的一些问题,但是我觉得并没有真正的解决的很好.后面会在学习过程中更新前面的那篇文章,加 ...
- vue中的路由独享守卫的理解
1.vue中路由独享守卫意思就是对这个路由有一个单独的守卫,因为他的守卫方式于其他的凡是不太同 独享守卫于前置守卫使用方法大致是一样的 在路由配置的时候进行配置, { path:'/login', c ...
- 【翻译】使用Vuex解决Vue中的身份验证
翻译原文链接:https://scotch.io/tutorials/handling-authentication-in-vue-using-vuex 我的翻译小站:https://www.zcfy ...
- vue中如何实现数据的双向绑定
vue中如何实现数据的双向绑定 实现视图变化数据跟着变:分两步,上面get中的为第二步(即再次读取的时候会调用get方法得到之前设置的值,以此来实现动态改变) 由于直接写obj.name = this ...
- vue中遇到的坑
如何解决在vue中替换图片.一个使用base64,而我们使用zepto之后,src找不到资源,因为已经打包了,难道强行写base64. 1. 引入文件时语法很重要! import "Foot ...
随机推荐
- 键盘键码Key Code
来源:https://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes Key Code backspace 8 t ...
- hdu 2844 Coins【多重背包】
题目链接:https://vjudge.net/contest/228640#problem/F 转载于:http://www.voidcn.com/article/p-mxcorksq-gh.htm ...
- 计蒜客 无脑博士的试管们 【dfs】
题目链接:https://nanti.jisuanke.com/t/31 题目大意: 无脑博士有三个容量分别是A,B,C 升的试管,A,B,C 分别是三个从 1 到20 的整数,最初,A 和 B 试管 ...
- caffe 利用VGG训练自己的数据
写这个是因为有童鞋在跑VGG的时候遇到各种问题,供参考一下. 网络结构 以VGG16为例,自己跑的细胞数据 solver.prototxt: net: "/media/dl/source/E ...
- sicp 习题
1.11 求f(n)=f(n-1)+2*f(n-2)+3*f(n-3) #lang racket (define (fff n) (define (fff-iter a b c n) (if (= n ...
- BZOJ.3809.Gty的二逼妹子序列(分块 莫队)
题目链接 /* 25832 kb 26964 ms 莫队+树状数组:增加/删除/查询 都是O(logn)的,总时间复杂度O(m*sqrt(n)*logn),卡不过 莫队+分块:这样查询虽然变成了sqr ...
- 判断socket连接是否失效
http://blog.csdn.net/jazywoo123/article/details/8693661 http://www.itnose.net/st/141698-pn11.html
- unity 背景无限循环滚动效果
背景无限循环滚动效果如下示: 步骤如下: 导入背景图片后,设置图片的格式,如下图: 2.图片格式也可以设置是Texture格式,但是Wrap Mode 一定要是Repeat[重复发生]:然后记得App ...
- Java多线程:CAS与java.util.concurrent.atomic
锁的几种概念 悲观锁 总是假设最坏的情况,每次获取数据都认为别人会修改,所以拿数据时会上锁,一直到释放锁不允许其他线程修改数据.Java中如synchronized和reentrantLock就是这种 ...
- 斯坦福CS231n学习--初识
课程主页:CS231n: Convolutional Neural Networks for Visual Recognition 关注其:Course Project主页 视频学习:云课堂 斯坦福C ...