<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
input{
width: 30px;
}
</style>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>
<body>
<div id="app">
<water-menu v-for="x in list" :key="x.id" v-bind:water="x"></water-menu>
<water-total v-bind:total="list"></water-total>
</div>
<template id="water">
<div>
<h2>{{water.name}}</h2>
<img v-bind:src="water.img" >
<p>单价 : <span>{{water.price}}</span>元</p>
<button type="button" @click="water.qty--" v-bind:disabled="water.qty==1">-</button>
<input type="text" v-model="water.qty" />
<button type="button" @click="water.qty++">+</button>
</div>
</template>
<template id="total">
<div>
<p>总价 : {{sumtotal}} 元</p>
</div>
</template>
<script type="text/javascript">
Vue.component('waterMenu',{
template:'#water',
props:['water']
})
Vue.component('waterTotal',{
template:'#total',
props:['total'],
computed:{
sumtotal(){
var total = 0;
// for(var i = 0;i<this.total.length;i++){
// total += this.total[i]['price'] * this.total[i]['qty'];
// }
this.total.forEach(function(item){
total += item['price']*item['qty'];
})
return total;
}
}
})
let vm = new Vue({
el:'#app',
data:{
list:[
{id:0,name:'柠檬水',img:'img/a.png',price:10,qty:1},
{id:1,name:'咖啡',img:'img/b.png',price:8,qty:1},
{id:2,name:'汽水',img:'img/c.png',price:6,qty:1},
]
}
})
</script>
</body>
</html>