0
I have a calculation of totals of certain values in a pagination. This calculation is in a computed Property inserted into a component. The problem that I have another similar component that needs to use the same computed Property. Following is part of the code below referring only to the calculation of totals (if you wish I can post the component in full):
// ... dentro de methods
vlr (param) {
let today = new Date()
today.setHours(0, 0, 0, 0)
let venc = new Date(param.ven)
venc.setHours(0, 0, 0, 0)
if (venc.getTime() >= today.getTime()) {
return parseFloat(param.total)
}
return param * 0.02
}
},
computed: {
getTotal (param) {
let total = {}
if (param.length) {
param.forEach(idx => {
idx.vlr = this.vlr(idx)
Object.keys(idx).forEach(key => {
if (!total[key]) {
total[key] = {
counter: 0,
sum: 0,
values: [],
uniqueCounter: 0
}
}
total[key].counter += 1
total[key].sum += idx[key]
if (total[key].values.indexOf(idx[key]) === -1) {
total[key].uniqueCounter += 1
total[key].values.push(idx[key])
}
})
})
}
return total
}
}
My question is exactly this. How to efficiently reuse a computed Property?
I tried to use with mixins, but I have not been successful so far for this situation.
Note: The code was made with Eslint.
Do you want to share values in Runtime, or just want to be more DRY and have the code shared by both components?
– Sergio
Sorry for the delay, it would be the second option.
– guastallaigor
Use the concept of bus. That’s my opinion. Yours App., within the created, call a method (bus.$on('methodname')) where it does the calculation. And within its components it gives a $Emit (using the bus) passing the values. The $Emit can even stay inside the computed.
– Marco Garcia