admin-vue/04-computed属性的使用/计算属性的数组操作.html

80 lines
1.8 KiB
HTML
Raw Normal View History

2022-10-15 10:05:34 +08:00
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
#app>div{
font-size: 20px;
text-align: center;
margin: 20px;
font-weight: bolder;
}
ul{
list-style: none;
text-align: center;
}
li{
text-align: center;
display: inline-block;
}
</style>
</head>
<body>
<div id="app">
<div>总数量:{{PrintSummer}}</div>
<ul>
<li :style="isChangeStyle()" v-for="list in item">{{list}}</li>
</ul>
</div>
</body>
<script src="../JavaScript/vue.min.js"></script>
<script>
let sum=0;
let vue=new Vue({
el:"#app",
data:{
item:[
{bid:01,name:"vue实验指南",count:66},
{bid:02,name:"C Primser",count:13},
{bid:01,name:"零基础学python",count:34},
{bid:01,name:"java程序设计",count:55},
{bid:01,name:"python从入门到精通",count:76},
],
isFontSize:"20px",
isColor:"#f00",
},
methods: {
isChangeStyle(){
return {fontSize:this.isFontSize,color:this.isColor}
}
},
computed:{
PrintSummer(){
//推荐写法: for(let book of this.item){
// sum+=book.count;
// }
// for(let book in this.item){
// sum+=this.item[book].count
// }
// return sum;
// for(let i=0;i<this.item.length;i++){
// sum+=this.item[i].count
// }
// return sum
for(let book of this.item){
sum+=book.count
}
return sum
}
}
})
</script>
</html>