使用Vue做个简单的评论 + localstorage存储
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
^_^,每天保持心情的愉悦。
使用Vue做个简单的评论 + localstorage存储的更多相关文章
- 使用Vue做评论+localStorage存储(js模块化)
未分模块化 html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> &l ...
- 用 Vue 做一个简单的购物app
前言 最近在学习Vue的使用.看了官方文档之后,感觉挺有意思的.于是着手做了一个简单的购物app.h5 与原生 app 交互的原理这是我第一次在这个网站上写分享,如有不当之处,请多多指教. 一整个项目 ...
- vue做的简单购物车
<code><!DOCTYPE html><html><head lang="en"> <meta charset=" ...
- vue 做一个简单的TodoList
目录结构 index.html <!DOCTYPE html> <html> <head> <meta charset="utf-8"&g ...
- 全栈前端入门必看 koa2+mysql+vue+vant 构建简单版移动端博客
koa2+mysql+vue+vant 构建简单版移动端博客 具体内容展示 开始正文 github地址 <br/> 觉得对你有帮助的话,可以star一下^_^必须安装:<br/> ...
- day 82 Vue学习二之vue结合项目简单使用、this指向问题
Vue学习二之vue结合项目简单使用.this指向问题 本节目录 一 阶段性项目流程梳理 二 vue切换图片 三 vue中使用ajax 四 vue实现音乐播放器 五 vue的计算属性和监听器 六 ...
- MUI框架-05-用MUI做一个简单App
MUI框架-05-用MUI做一个简单App MUI 是一个前端框架,前端框架就像 Bootstrap,EasyUI,Vue ,为了做 app 呢,就有了更加高效的 MUI,我觉得前端框架有很多,也没有 ...
- 前台vue的使用简单小结
前台vue的使用简单小结 本项目要求:安装有node.js 6.0以及以上安装npm使用vue.js官方安装方法初始化项目npm install安装VueResurce:npm install vue ...
- Vue 变异方法splice删除评论功能
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
随机推荐
- caoz的梦呓:信息安全攻防杂谈
猫宁!!! 参考链接:https://mp.weixin.qq.com/s/O0zLvuWPRPIeqnRooNEFYA 旧文我提过一句话,信息安全防御这事,在业内,三分靠技术,七分靠人脉. 在知识星 ...
- Linux C\C++基础——数组形参的使用
1.数组形参 ]) void fun(int a[]) void fun(int *a) ],int n) void fun(char*p[],int n) void fun(char**p,int ...
- 【Linux开发】linux设备驱动归纳总结(三):3.设备驱动面向对象思想和lseek的实现
linux设备驱动归纳总结(三):3.设备驱动面向对象思想和lseek的实现 一.结构体struct file和struct inode 在之前写的函数,全部是定义了一些零散的全局变量.有没有办法整合 ...
- 第三周课程总结&实验报告一
实验一 Java开发环境与简单Java程序 一.实验目的 熟悉JDK开发环境 熟练掌握结构化程序设计方法 二.实验内容 1.在此处输入标题打印输出所有的"水仙花数",所谓" ...
- PTA(Basic Level)1028.人口普查
某城镇进行人口普查,得到了全体居民的生日.现请你写个程序,找出镇上最年长和最年轻的人. 这里确保每个输入的日期都是合法的,但不一定是合理的--假设已知镇上没有超过 200 岁的老人,而今天是 2014 ...
- 客户端实现WebService服务接口
首先,要获得搭建好的WebService服务的WSDL,如要实现国内手机号码归属地查询WEB服务,其WSDL为:http://ws.webxml.com.cn/WebServices/MobileCo ...
- <<C++ Primer>> 第 7 章 类
术语表 第 7 章 类 抽象数据类型(abstract data type): 封装(隐藏)了实现细节的数据结构. 访问说明符(access specifier): 包括关键字 public 和 ...
- POJ - 3469 Dual Core CPU (最小割)
(点击此处查看原题) 题意介绍 在一个由核A和核B组成的双核CPU上执行N个任务,任务i在核A上执行,花费Ai,在核B上执行,花费为Bi,而某两个任务之间可能需要进数据交互,如果两个任务在同一个核上执 ...
- 解决 mysql (10038)
1.授权 mysql>grant all privileges on *.* to 'root'@'%' identified by 'youpassword' with grant o ...
- Iterable<T>接口
https://docs.oracle.com/javase/8/docs/api/java/lang/Iterable.html public interface Iterable<T> ...