The issue with the current code is that lists is a ref object, so you need to access the actual array using lists.value. Also, when using forEach, the this keyword inside the callback function refers to the function itself, not the Vue instance. To fix this, you can use an arrow function instead, which preserves the this context. Here's the updated code:
export default {
data() {
return {
lists: ref([]),
totalComplete: 0,
}
},
mounted() {
this.getLists();
this.lists.value.forEach((list) => {
this.totalComplete += list.num_complete;
});
},
methods: {
getLists() {
axios.get(route('lists')).then((response) => {
this.lists.value = response.data.lists;
});
}
},
}