1. 通过前面学习的基础知识,实现一个评论列表
1.1 代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>vue小案例-评论列表案例</title>
<style>
* {
padding: 0;
margin: 0;
outline: none;
box-sizing: border-box;
}
.out {
width: 550px;
min-height: 200px;
margin: 100px auto;
border: 1px solid #eee;
border-radius: 5px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
padding: 30px;
}
.search {
display: flex;
padding-bottom: 30px;
}
.search .ipt {
height: 36px;
border: none;
background: #eee;
border-radius: 3px;
padding: 0 10px;
font-size: 16px;
color: #666;
width: 80%;
letter-spacing: 0.1em;
}
.search .btn {
height: 36px;
width: 20%;
border: none;
background: #4fc08d;
color: #fff;
font-size: 16px;
cursor: pointer;
}
.out .row {
border-bottom: 1px solid #eee;
border-top: 1px solid #eee;
height: 40px;
display: flex;
align-items: center;
font-size: 16px;
justify-content: space-between;
margin-top: -1px;
color: #333;
}
.out .row .close {
font-size: 22px;
font-family: "宋体";
color: #ff976a;
display: none;
cursor: pointer;
}
.out .row:hover .close {
display: block;
}
.out .num {
padding-top: 20px;
color: #555;
text-align: center;
}
.out .num b {
display: inline-block;
padding: 0 3px;
}
.out .comment {
font-size: 16px;
color: #999;
text-align: center;
}
</style>
</head>
<body>
<div id="app">
<div class="out">
<div class="search">
<!-- v-model="iptValue" 表示与Vue实例化中的data数据实现双向绑定 -->
<!-- @keyup.enter="pushData" 表示点击回车的时候提交数据 -->
<input type="text" class="ipt" v-model="iptValue" @keyup.enter="pushData">
<!-- @click="pushData" 表示当点击按钮的时候提交数据 -->
<input type="button" class="btn" value="评论" @click="pushData">
</div>
<!-- v-for="(item,index) in arrs" item表示在arrs数组中获取到每一项值,index表示数组下标索引 -->
<div class="row" v-for="(item,index) in arrs">
<!-- 通过插值表达式显示在界面上 -->
{{item}}
<!-- 当点击x的时候移除到数组中数组下标匹配的元素 -->
<div class="close" @click="btnClose(index)">x</div>
</div>
<!-- v-if="arrs.length" 当其不为0的时候就是true,将其显示在界面上,反之为false,将其隐藏掉 -->
<div class="num" v-if="arrs.length">共<b>{{arrs.length}}</b>条评论</div>
<div class="comment" v-else>暂无评论</div>
</div>
</div>
<script src="../js/vue.js"></script>
<script>
new Vue({
el: "#app",
data: {
arrs:[
"评论111",
"Vue学习",
"Vue学习小案例"
],
iptValue:""
},
methods: {
//点击删除某一个
btnClose(index){
console.log(index);
//删除操作
this.arrs.splice(index,1);
},
//向数组中追加数据
pushData(){
this.arrs.unshift(this.iptValue);
//清空输入框
this.iptValue=""
}
}
})
</script>
</body>
</html>