localstorage和vue结合使用
父组件
<template>
<div class="hello">
<p>Original message:"{{message}}"</p><br/>
<input v-model="newTodoText" v-on:keyup.enter="addNewText" placeholder="Add a todo">
<ul>
<li is="tab2" v-for="(todo,index) in todos" :key="todo.id" :title="todo.title" @remove="todos.splice(index,1)">
</li>
</ul>
</div>
</template> <script>
import store from '@/store/todo_list.js'
import Tab2 from '@/components/tab2/tab2'
export default {
components:{Tab2},
data () {
return {
message:"Hello",
newTodoText:'',
todos: store.fetch(),
nextTodoId: 4
}
},
watch:{
todos:function(val){
console.log(val);
store.save(val);
}
},
computed:{ },
methods: {
addNewText: function () {
this.todos.push({
id: this.nextTodoId++,
title: this.newTodoText
})
this.newTodoText = ''
}
}
}
</script> <!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
font-weight: normal;
} ul {
list-style-type: none;
padding: 0;
} li {
display: inline-block;
margin: 0 10px;
} a {
color: #42b983;
}
.active{
color:red;
}
.text-danger{
color:green;
}
</style>
todo_list.js
const STORAGE_KEY = 'todos-vuejs'
export default {
fetch: function() {
return window.JSON.parse(window.localStorage.getItem(STORAGE_KEY) || '[]')
},
save: function(items) {
window.localStorage.setItem(STORAGE_KEY, window.JSON.stringify(items))
}
}
子组件返回删除按钮
<template>
<li>{{title}}
<button v-on:click="close">X</button>
</li>
</template>
<script>
export default{
props:['title'],
data(){
return{ }
},
methods:{
close(){
this.$emit('remove');
}
}
}
</script>
<style>
</style>
效果

接一篇自己写的
引入jquery和bootstrap查考我vue分类里面的随便,
我这里在页面里面构建了自己的storage全局工具类
<template>
<div class="container">
<form role="form">
<div class="form-group">
<label for="username">用户名:</label>
<input type="text" id="username" class="form-control" placeholder="输入用户名"
v-model.trim="username">
</div>
<div class="form-group">
<label for="age">年 龄:</label>
<input type="text" id="age" class="form-control" placeholder="输入年龄" v-model.trim="age">
</div>
<div class="form-group">
<input type="button" value="添加" class="btn btn-primary" @click="add()">
<input type="button" value="重置" class="btn btn-danger">
</div>
</form>
<hr>
<table class="table table-bordered table-hover">
<caption class="h2 text-info">用户信息表</caption>
<tr class="text-danger">
<th class="text-center">序号</th>
<th class="text-center">名字</th>
<th class="text-center">年龄</th>
<th class="text-center">操作</th>
</tr>
<tr class="text-center" v-for="(item,index) in myData">
<td>{{index}}</td>
<td>{{item.name}}</td>
<td>{{item.age}}</td>
<td>
<button class="btn btn-primary btn-sm" data-toggle="modal" data-target="#layer"
@click="nowIndex==index">删除</button>
</td>
</tr> <tr v-show="myData.length!=0">
<td colspan="4" class="text-right">
<button class="btn btn-danger btn-sm" data-toggle="modal" data-target="#layer"
v-on:click="nowIndex=-2">删除全部</button>
</td>
</tr>
<tr v-show="myData.length==0">
<td colspan="4" class="text-center text-muted">
<p>暂无数据....</p>
</td>
</tr>
</table> <!--模态框 弹出框-->
<div role="dialog" class="modal fade bs-example-modal-sm" id="layer">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">
<span>×</span>
</button>
<h4 class="modal-title">确认删除么?</h4>
</div>
<div class="modal-body text-right">
<button data-dismiss="modal" class="btn btn-primary btn-sm">取消</button>
<button data-dismiss="modal" class="btn btn-danger btn-sm"
@click="deleteMsg(nowIndex)">确认</button>
</div>
</div>
</div>
</div>
</div>
</template> <script>
export default {
name: 'HelloWorld',
data () {
return {
myData:[],
username:'',
age:'',
nowIndex:-100
}
},
created(){
if(this.$storage.getStorage("myData-list")){
var index=this.$storage.getStorage("myData-list");
this.myData=index;
}
},
watch:{
myData:function(nowVal,oldVal){
this.$storage.setStorage("myData-list",nowVal);
}
},
methods:{
add(){
if(this.username.length==0 || this.age.length==0){
alert("用户名或年龄不为空");
}else{
this.myData.push({
name:this.username,
age:this.age
});
this.username="";
this.age=""
}
},
deleteMsg(n){
if(n==-2){
this.myData=[];
}else{
this.myData.splice(n,1);
}
}
}
}
</script>

参考链接:http://www.cnblogs.com/yingzi1028/p/7774954.html
localstorage和vue结合使用的更多相关文章
- localstorage和vue结合使用2
html <template> <div class="hello"> <div class="page-top"> < ...
- vue中使用localStorage存储信息
一 什么是localStorage 对浏览器来说,使用 Web Storage 存储键值对比存储 Cookie 方式更直观,而且容量更大,它包含两种:localStorage 和 sessionSto ...
- Vue应用框架整合与实战--Vue技术生态圈篇
实用框架以及工具 UI组件 开发框架 实用库 服务端 辅助工具 应用实例 Demo示例 UI组件 Element-UI ★13489 - 饿了么出品的Vue2的web UI工具套件 Vux ★8133 ...
- 【转载】 github vue 高星项目
内容 UI组件 开发框架 实用库 服务端 辅助工具 应用实例 Demo示例 UI组件 element ★13489 - 饿了么出品的Vue2的web UI工具套件 Vux ★8133 - 基于Vue和 ...
- VUE组件汇总
内容 UI组件 开发框架 实用库 服务端 辅助工具 应用实例 Demo示例 UI组件 element ★13489 - 饿了么出品的Vue2的web UI工具套件 Vux ★8133 - 基于Vue和 ...
- 【分享】Vue 资源典藏(UI组件、开发框架、服务端、辅助工具、应用实例、Demo示例)
Vue 资源典藏,包括:UI组件 开发框架 服务端 辅助工具 应用实例 Demo示例 element ★11612 - 饿了么出品的Vue2的web UI工具套件 Vux ★7503 - 基于Vue和 ...
- Vue开源项目汇总(史上最全)(转)
目录 UI组件 开发框架 实用库 服务端 辅助工具 应用实例 Demo示例 UI组件 element ★13489 - 饿了么出品的Vue2的web UI工具套件 Vux ★8133 - 基于Vue和 ...
- vue(6)生态
来自:https://www.jianshu.com/p/22a99426b524?utm_campaign=maleskine&utm_content=note&utm_medium ...
- Vue相关开源项目库汇总 http://www.opendigg.com/tags/front-vue
awesome-github-vue 是由OpenDigg整理并维护的Vue相关开源项目库集合.我们会定期同步OpenDigg上的项目到这里,也欢迎各位提交项目给我们. 如果收录的项目有错误,可以通过 ...
随机推荐
- [Redis]Redis的快速配置使用(图)
--------------------------------------------------------------------------------------------------- ...
- Delphi编写Shell扩展
用delphi创建一个外壳扩展(Shell Extension)程序的基本步骤如下: (1) 创建一个 ActiveX Library 工程,命名为“CloudUpload“(2) 创建一个新的自动化 ...
- SQL中ISNULL的问题。
今天在写SQL代码的时候写了个 ISNULL(变量1,变量2),返回的结果居然是 "*" ,这个星号,郁闷了很久. 代码大意如下: ) declare @str2 int sele ...
- 吴裕雄 python 机器学习-NBYS(2)
import matplotlib import numpy as np import matplotlib.pyplot as plt n = 1000 xcord0 = [] ycord0 = [ ...
- ssl 的jks 生成工具
https://www.myssl.cn/tools/merge-jks-cert.html 通过key 私钥 ,和公钥pem 生成jks
- Java KeyNote
[Java KeyNote] 1.把一个ArrayList拷贝到另一个ArrayList. ArrayList list1=new ArrayList(); ArrayList list2=new A ...
- 创建java项目思路
一.搭建 1.创建搭建项目 2.创建分层 二.理解项目(理清总体思路) 1.是否有共同部分(过滤或者拦截) 常用量 (static) 2.搭建单表基本增(是否需要返回值) 删(条件) 查(条 ...
- Mysql 5.7 忘记root密码或重置密码的详细方法
在Centos中安装完MySQL数据库以后,不知道密码,这可怎么办,下面给大家说一下怎么重置密码 在Centos中安装完MySQL数据库以后,不知道密码,这可怎么办,下面给大家说一下怎么重置密码 1. ...
- python生成Excel图表(通过xlsxwriter)
前面介绍了pandas的简单用法,pandas的数据可以通过matlab第三方包将数据生成报表,但是我想将报表生成在Excel中,这时候就可以借助xlsxwriter第三方包来生成图标 缺点:xl ...
- leetcode 链表类型题总结
链表测试框架示例: // leetcodeList.cpp : 定义控制台应用程序的入口点.vs2013 测试通过 // #include "stdafx.h" #include ...