源代码
<div id="app">
<h2>total:{{totalPrice}}</h2>
</div>
<script>
const app=new Vue({
el:'#app',
data:{
// 定义数组
books:[
{id:01,name:'vue',price:80},
{id:02,name:'c++',price:45},
{id:03,name:'python',price:120},
{id:04,name:'js',price:128},
{id:05,name:'c#',price:116}
]
},
//计算属性
computed:{
totalPrice:function(){
let result=0
for(let i=0;i<this.books.length;i++)
{
result += this.books[i].price
}
return result
}
},
})
结果: