我不知道如何使用 $dispatch 方法将数据从 Vue 子级发送到 Vue 父级。我有一个包含多个实例的组件,如下所示:
Vue.component('receipt', {
template: '#receipt-template',
data: function() {
return {
tip: ''
};
},
methods: {
addSale: function() {
this.sales.push(
{amount: 1, desc: '', price: 0.00}
);
},
removeSale: function(index) {
this.sales.splice(index, 1)
}
},
computed: {
subtotal: function() {
var result = 0;
this.sales.forEach(function (sale) {
return result += +sale.price;
});
var subtotal = Math.round(100 * result) / 100;
return subtotal.toFixed(2);
},
tax: function() {
var tax = this.subtotal * .08;
return tax.toFixed(2);
},
total: function() {
var total = Number(this.subtotal) + Number(this.tax) + Number(this.tip);
return total.toFixed(2);
this.$dispatch(this.total);
}
},
props: [ 'header', 'date', 'sales' ]
})
我的 Vue 实例看起来像:
var vm = new Vue({
el: '#content',
data: {
sales1: [
{amount: 1, desc: "Dante's Inferno", price: 13.99},
{amount: 1, desc: "Espresso", price: 5.25},
{amount: 1, desc: "The Sun Also Rises", price: 11.99},
{amount: 1, desc: "Spanish Coffee", price: 1.99}
],
sales2: [
{amount: 1, desc: "Huckleberry Finn", price: 14.95},
{amount: 1, desc: "Americano", price: 2.29},
{amount: 1, desc: "Pride & Prejudice", price: 12.95},
{amount: 1, desc: "Black Tea Latte", price: 4.25},
{amount: 1, desc: "Scone", price: 3.25}
],
company: 'Between The Covers & Grinders Cafe'
},
computed: {
grand: function() {
}
}
})
我有“收据”组件的多个实例,因此从组件的计算“总计”函数计算出多个值。如何调度组件实例“总计”函数的值并在父实例中获取我的“总计”