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上的项目到这里,也欢迎各位提交项目给我们. 如果收录的项目有错误,可以通过 ...
随机推荐
- Linux tr命令使用方法
tr命令主要用于删除文件中控制字符或进行字符转换.本文主要介绍tr命令的基本语法和使用实例. tr基本语法 tr命令格式:tr [ -d ] [ -c ] [ -s ] [ 字符串1 ] [ 字符串2 ...
- webpack 自动发现 entry 的配置和引用方式
假定我们的项目目录为如下的样子: - root/ - assets/ - app/ - global.js - index/ - index.js - auth/ - login.js - regis ...
- MYSQL数据库中,常见的数据类型有哪些?它们与java中的数据类型如何对应
A.常规 映射 integer 或者 int int 或者 java.lang.Integer INTEGER 4 字节 long long Long BIGINT 8 字节 short short ...
- [PHP]PHP的session机制,配置与高级应用
---------------------------------------------------------------------------------------------------- ...
- django 认证系统--1
django的认证系统提供认证和授权两种功能 认证系统包括如下部分: 1.Users 2.Permissions 主要是以 YES/NO 的形式反映一个用户是否能够做某事 3.Groups:就是对多个 ...
- serclet监听器
1:监听servlet上下文 2:监听会话 3:监听请求 使用,必须是实现对应的接口,然后在web.xml中配置自己写的监听器的实现类 过滤器之后,servlet之前(有待深入研究) 下一集预告:过滤 ...
- Chrome 不能访问tensorboard解决
Chrome 不能访问tensorboard解决 Run: Cmd Result: C:\Users\think>tensorboard --logdir=C:\Users\think\sour ...
- IE 11中 onpropertychange失效
https://msdn.microsoft.com/zh-cn/library/ie/dn265032(v=vs.85).aspx 将突变事件和属性更改事件迁移到突变观察者 Internet Exp ...
- canvas元素绘制太极图
<!DOCTYPE html><html><head> <title>canvas example</title> <meta cha ...
- ServiceWorker.state
[ServiceWorker.state] ServiceWorker.state The state read-only property of the ServiceWorker interfac ...