父组件

 <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>&times;</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结合使用的更多相关文章

  1. localstorage和vue结合使用2

    html <template> <div class="hello"> <div class="page-top"> < ...

  2. vue中使用localStorage存储信息

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

  3. Vue应用框架整合与实战--Vue技术生态圈篇

    实用框架以及工具 UI组件 开发框架 实用库 服务端 辅助工具 应用实例 Demo示例 UI组件 Element-UI ★13489 - 饿了么出品的Vue2的web UI工具套件 Vux ★8133 ...

  4. 【转载】 github vue 高星项目

    内容 UI组件 开发框架 实用库 服务端 辅助工具 应用实例 Demo示例 UI组件 element ★13489 - 饿了么出品的Vue2的web UI工具套件 Vux ★8133 - 基于Vue和 ...

  5. VUE组件汇总

    内容 UI组件 开发框架 实用库 服务端 辅助工具 应用实例 Demo示例 UI组件 element ★13489 - 饿了么出品的Vue2的web UI工具套件 Vux ★8133 - 基于Vue和 ...

  6. 【分享】Vue 资源典藏(UI组件、开发框架、服务端、辅助工具、应用实例、Demo示例)

    Vue 资源典藏,包括:UI组件 开发框架 服务端 辅助工具 应用实例 Demo示例 element ★11612 - 饿了么出品的Vue2的web UI工具套件 Vux ★7503 - 基于Vue和 ...

  7. Vue开源项目汇总(史上最全)(转)

    目录 UI组件 开发框架 实用库 服务端 辅助工具 应用实例 Demo示例 UI组件 element ★13489 - 饿了么出品的Vue2的web UI工具套件 Vux ★8133 - 基于Vue和 ...

  8. vue(6)生态

    来自:https://www.jianshu.com/p/22a99426b524?utm_campaign=maleskine&utm_content=note&utm_medium ...

  9. Vue相关开源项目库汇总 http://www.opendigg.com/tags/front-vue

    awesome-github-vue 是由OpenDigg整理并维护的Vue相关开源项目库集合.我们会定期同步OpenDigg上的项目到这里,也欢迎各位提交项目给我们. 如果收录的项目有错误,可以通过 ...

随机推荐

  1. 吴裕雄 25-MySQL 临时表

    MySQL 临时表MySQL 临时表在我们需要保存一些临时数据时是非常有用的.临时表只在当前连接可见,当关闭连接时,Mysql会自动删除表并释放所有空间.临时表在MySQL 3.23版本中添加,如果你 ...

  2. 判断素数(翁凯男神MOOC)

    从2到x-1测试是否可以整除 int isPrime(int x); int main(int argc, char **argv) { int x; scanf("%d",&am ...

  3. php图片转base64

    /*读取问价家图片生澈哥哥js文件 */header("Access-Control-Allow-Origin: *");$i=0;$handle = opendir('./ima ...

  4. linux安装node简单方法

    1.去官网下载和自己系统匹配的文件: 英文网址:https://nodejs.org/en/download/ 中文网址:http://nodejs.cn/download/ 通过  uname -a ...

  5. http协议解析过程

    HTTP是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统. 基于HTTP协议的客户端/服务器请求响应机制的信息交换过程包含下面几个步骤: 1)     建立连接:客 ...

  6. perl5

    1.perl包加入环境 export PERL5LIB=/export/personal1/wanglh/.software/perl/lib:$PERL5LIB

  7. 05_ssm基础(四)之Spring与持久层的整合

    30.31.spring对jdbc的支持jdbcTemplate 使用Spring的JDBC准备:  1):拷贝jar:       mysql-connector-java-5.1.11.jar:M ...

  8. appium的内存泄露问题

    appium的一个内存泄露的问题 标签(空格分隔): appium 我们在做移动端的测试后时候,经常会用到appium 但是有时候我们跑一个小时候/2个小时候时候,会遇到appium报错的信息: ap ...

  9. pta l3-20(至多删三个字符)

    题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805046946938880 题意:给定一个长度<=106 ...

  10. CentOS 查找某个软件安装路径

    1.通过rpm查看 查看软件是否安装.首先我们需要查看软件是否已经安装,或者说查看安装的软件包名称.如查找是否安装mysql 2.接着根据 rpm -ql 列出软件包安装的文件 3.综合上述以上的问题 ...