C
C
camradee2022-01-07 18:47:41
Vue.js
camradee, 2022-01-07 18:47:41

Why is vue computed property called multiple times?

I am a beginner in vue and I don’t really understand the deep processes that take place in it, and now I ran into a problem - I need to send some statistics, for this you can fasten watch to a getter or register sending these statistics to a computed property. In my case, the most suitable products property gives a list of products on the page, it is called only once in the component through the v-for="product in products" loop, but if something is output to the console, then this something will be displayed many times. Accordingly, my statistics will be sent many times. If that this component is wrapped lazy-hydration. But I don't think it's related. I can assume the first call sees the cached version, then calls again getting fresh, but in the console calls up to 4 times. What's going on, is this normal?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Aetae, 2022-01-07
@Aetae

What nafig sending statistics to computed? computedshould not have side effects, only give value.
More code is needed, it is not clear from the text what is happening there.
If you simply describe how it works computed: the first time it is accessed, the first calculation is performed, and also, conditionally , it is assigned watchto all reactive properties that were accessed inside it. Accordingly, recalculation computedoccurs only if one of these properties has changed.
PS With SSR computed on the server side, it's not cached at all, like, ie. recalculated on every call. Infa is not 100%.)

W
Wispik, 2022-01-07
@Wispik

How difficult it is to show a code example where and what does not work. It’s better to write a bunch of sentences, from which Figs can be understood ((
But, if I still understood the question, then you need to do:
1. Create a computed property that considers what is needed
2. Make a watch behind this property and send statistics in it

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question