1、引入Vue.js

2、编写代码

代码

<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Vue 和 localstorage 的结合使用(版本一)</title>
<style>
.main {
border: 3px solid antiquewhite;
width: 500px;
height: 600px;
margin: auto;
} .top {
border: 2px solid aquamarine;
width: 450px;
height: 200px;
margin: auto;
margin-top: 10px;
text-align: center;
} .content {
border: 4px solid aliceblue;
width: 450px;
height: 350px;
margin: auto;
margin-top: 20px;
} .top input {
margin-top: 10px;
height: 25px;
width: 60%;
margin-left: 20px;
} .top label {
display: flex;
margin-left: 35px;
height: 100px;
} .top label>textarea {
margin-left: 20px;
width: 270px;
} .top button {
margin-left: 270px;
background: #7FFFD4;
width: 65px;
} .content>div div {
border: 3px solid #F0F8FF;
height: 30px;
margin-top: 4px;
padding-top: 10px;
} .content>div>div a {
float: right;
}
</style>
</head>
<body> <script src="js/vue.js"></script>
<div id="app" class="main"> <div class="top">
<div>
用户名:<input type="text" v-model="author" />
<p></p>
<label>
<span>评论内容:</span>
<textarea v-model="content"></textarea>
</label>
<p><button @click='saveData'>发布</button></p>
</div>
</div> <div class="content">
<div v-for="data in datas" >
<div>{{data.key}}:{{data.value}} <a href @click.prevent='del(data.id)'>删除此评论</a></div>
</div>
</div> </div>
<script>
new Vue({
el:"#app",
data:{
datas:[],
author:"",
content:""
},
methods:{
saveData(){
//将用户名存储到localstorage中
localStorage.setItem("input_author",this.author); if(this.author.trim() === ""){
return alert("小兄弟,请先输入用户名啊!!!!");
} if(this.content.trim() === ""){
return alert("大兄弟,你输入一下内容呗!!!!!");
}
//将用户名和对应的内容push到数组中
this.datas.push({
id:+new Date(),
key:this.author,
value:this.content
});
//更新
this.updateData(); },
updateData(){
//将数组序列化成字符串存入localstorage中
localStorage.setItem("author_content",JSON.stringify(this.datas));
},
del(id){
//根据id过滤datas中的数组
this.datas = this.datas.filter( (c) => c.id != id );//简写 // this.datas= this.datas.filter(function(c){
// return c.id != id;
// }) //更新数据
this.updateData();
}
},
created(){
//取到上一次的用户名
var author = localStorage.getItem("input_author");
if(author){
this.author = author;
}
//取出localstorage中的评论数据
var content = localStorage.getItem("author_content");
if(content){
//将存在localstorage中的数据取出,并序列化储成对象存入数组中
this.datas = JSON.parse(content);
}
}
});
</script>
</body>
</html>

运行结果:

Vue示例下载地址(Vue,Vue模块化):https://github.com/oukele/VueAndLocalstorage

^_^,每天保持心情的愉悦。

最新文章

  1. ArcGis 字段计算表达式写法注意事项
  2. 史上最强大的40多个纯CSS绘制的图形
  3. JavaWeb学习笔记——JavaEE基础知识
  4. MySQL忘记密码,或:root密码重置报错:mysqladmin: connect to server at &#39;localhost&#39; failed的解决方案
  5. SpringMVC3的ResponseBody返回字符串(JSON)乱码问题解决
  6. CAS SSO对手机应用支持的一种思路
  7. IOS 类似网易新闻客户端内容滚动菜单跟随居中组件
  8. mysql 数字字段的类型选择
  9. nodejs笔记2 --关于nodejs最新启动方式
  10. RabbitMQ基础
  11. Js前端传递json数组至服务器端并解析的实现。
  12. php中AJAX请求中使用post和get请求的区别
  13. Echarts 南海诸岛简图显示位置调整
  14. NTFS文件系统详细分析
  15. electron-builder 由于网络原因无法下载问题解决
  16. JS 正则表达式深入
  17. 【bootstrap组件】几个常用的好用bs组件
  18. Speech Synthesis
  19. ThinkPHP-5.0.23新的RCE漏洞测试和POC
  20. 2018.10.01 NOIP模拟 卡牌游戏(贪心)

热门文章

  1. 自然语言处理NLP学习笔记三:使用Django做一个NLP的Web站点
  2. python笔记之元祖
  3. python+selenium调用JavaScript
  4. sqlalchemy orm的cascade的参数
  5. 微信小程序打开地图选择位置
  6. MYSQL 优化--inner buffer 与关联查询变等值查询
  7. typescript中类型断言好理解也好用
  8. AC自动机练习题1:地图匹配
  9. sklearn-GDBT
  10. Hive 教程(八)-hiveserver2