我有一個組件,其中包含單位價格,數量並希望計算不在組件內部的總計。我能做些什麼來實現這一目標? 每個組件都是動態生成的,下面是一個示例代碼。 謝謝!從另一個組件計算VueJs
<table>
<tr is="item-grid" v-for='index in counter'></tr>
<tr><td v-text="sub_total"></td></tr>
<button @click="counter++" type="button">TEST ADD ROW</button>
</table>
<template id="item-template">
<tr>
<td><input type="text" class="form-control" v-model="inventory_name" readonly/></td>
<td><input type="text" class="form-control" v-model="sku"/></td>
<td><input type="text" class="form-control" v-model="qty"/></td>
<td><input v-model="unit_price"></input></td>
<td><span v-text="unit_total"></span></td>
<td><button @click="remove(this)" type="button">delete</button></td>
</tr>
</template>
<script>
var Item = {
template: '#item-template',
data: function(){
return {'qty': 0, 'unit_price': 0.00, 'inventory_name': '', 'sku': ''}
},
methods:{
remove: function(a){
console.log(a);
}
},
computed: {
unit_total: function(){
return this.qty * this.unit_price;
}
}
}
new Vue({
el: '#app',
props: ['item','sub_total','tax','grand_total'],
data: {
'counter': 1,
},
components: {
'item-grid': Item,
},
// computed: {
// sub_total: function(){
// return Item.unit_total++;
// }
// }
});
</script>
那麼最簡單的方法是保持將被用於外部對象計算的值,然後,你需要做的的計算到位使用它們從該對象。 –
@BelminBedak所有組件都是動態生成的,是否可以重新循環它們來計算? –