console
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
</head>
<body>
<div id="app">
<h3> Write your comment</h3>
<textarea v-model="text" style="height: 50px;width: 300px;"></textarea>
<br>
<button @click="addComment()">添加评论</button>
<hr>
<div v-for="item in comments">
<span>{{item.id}}</span>
<button @click="showComments(item.id)">展开折叠</button>
<div v-show="item.isFold">
<h3>{{item.content}}</h3>
<button @click="showReplyContent(item.id)">回复评论</button>
<div v-show="item.isReply">
<input type="text" v-model="item.replyInput">
<button @click="submitReply(item.id)">submit</button>
</div>
<h5 v-for="subItem in item.replyContent">
{{subItem}}
</h5>
</div>
<hr>
</div>
</div>
<script>
var app = new Vue({
el: '#app',
data: {
text:'',
count:0,
comments:[
],
arr:[]
},
methods:{
addComment(){
if(!this.text){
this.text = "No Content";
}
let obj = {};
obj.content = this.text;
obj.id = ++this.count;
obj.isFold = false;
obj.isReply = false;
obj.replyContent = [];
replyInput = '';
this.comments.push(obj);
window.sessionStorage.setItem("comment",JSON.stringify(this.comments));
this.text = "";
},
showComments(id){
let item = this.comments.filter(x=>{
return x.id == id
})[0];
item.isFold = !item.isFold;
window.sessionStorage.setItem("comment",JSON.stringify(this.comments));
},
showReplyContent(id){
let item = this.comments.filter(x=>{
return x.id == id
})[0];
item.isReply = !item.isReply;
window.sessionStorage.setItem("comment",JSON.stringify(this.comments));
},
submitReply(id){
let item = this.comments.filter(x=>{
return x.id == id
})[0];
item.replyContent.push(item.replyInput);
item.replyInput = "";
item.isReply = false;
window.sessionStorage.setItem("comment",JSON.stringify(this.comments));
},
},
mounted(){
if(JSON.parse(window.sessionStorage.getItem("comment")) == null){
this.comments = [];
}else{
this.comments = JSON.parse(window.sessionStorage.getItem("comment"));
this.count = this.comments[this.comments.length-1].id;
}
},
})
</script>
</body>
</html>