<!DOCTYPE html>
<html lang="en">
<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>Document</title>
</head>
<style>
.active {
color: red;
}
</style>
<body>
<div id="app">
<h2>总价格:{{totalPrice}}</h2>
</div>
<script src="./js/vue.js"></script>
<script>
var app = new Vue({
el: '#app',
data: {
books: [{
id: 110,
name: 'geyao',
price: '119'
}, {
id: 110,
name: 'geyao',
price: '119'
}, {
id: 110,
name: 'geyao',
price: '119'
}, {
id: 110,
name: 'geyo',
price: '119'
}, ]
},
methods: {
},
computed: {
totalPrice: function() {
let result = 0
for (let i = 0; i < this.books.length; i++) {
result += this.books[i].price
}
return result
}
}
})
</script>
</body>
</html>
运行结果