|
|
|
|
|
by dglass
1037 days ago
|
|
Disagree. I would argue your example is hard to read compared to the equivalent in Vue, and it mixes business logic (computing the running total) with the rendering logic. <script setup>
const items = [1,2,3,4,5,6]
let runningTotal = 0
const getRunningTotal = (item) => {
runningTotal += item
return runningTotal
}
</script>
<template>
<ul>
<li v-for="item in items">
Item {{item}}; running total {{ getRunningTotal(item) }}
</li>
</ul>
</template>
No need for a computed property. Plus, getRunningTotal can be unit tested, or extracted to another file for reusability. |
|
If you're after keeping the concerns separate you would need to precompute the running total for each item.
See also: mustache(5) which completely embodies this philosophy.