计算属性,computed类似methods中的方法,但是在调用的时候不需要小括号调用
<!DOCTYPE html>
<html lang="zh-CN">
<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>
<script src="js/vue.js"></script>
</head>
<body>
<div id="app">
<h2>书籍的总价:{{totlePrice}}</h2>
</div>
<script>
var vm = new Vue({
el: '#app',
data: {
books: [
{id: 110, name: 'js高级', price: 123},
{id: 110, name: 'js高级', price: 123},
{id: 110, name: 'js高级', price: 123},
{id: 110, name: 'js高级', price: 123},
{id: 110, name: 'js高级', price: 123},
]
},
methods: {},
computed: {
totlePrice() {
var totle = 0;
// for(let book of this.books) {
// totle += book.price;
// }
// for(let i = 0; i < this.books.length; i++) {
// totle += this.books[i].price;
// }
for(let i in this.books) {
totle += this.books[i].price;
}
return totle;
}
}
});
</script>
</body>
</html>
网友评论