The use of compute
First of all, you need to know how to use computed data. The website simply introduces its caching function. It calculates the return value based on bidirectional binding data and triggers recalculation when the dependent value changes
It can only be used to calculate values and can not be used for other purposes. It can listen on multiple data depending on two-way binding. That is, we all know that watch can only monitor one data object, but sometimes when we want to monitor multiple objects, we need to write many watch methods and trigger many times. In many cases, we need to monitor more than one data and monitor asynchronously, so we can use computed to solve the problem. If you don’t understand what this means, you can take a look at the following example.
Here I’m listening for two bidirectional binding data, testData and test3, but test3 will change in the next update after the mount, and when that changes, computed methods will be triggered again, which means I’m listening for the change. In contrast, if I’m monitoring two two-way binding data, the result is that I’m monitoring changes in a single data using watch, and monitoring changes in multiple data using computed, for example, if someone thinks I’m not changing another data, I’m just monitoring one data, so I changed the code. Let another statistic change as well.
So you can see that this fires three times, and why three times is that computed itself is called between the lifetime of Created and Mounted, and this is the first time that test3 in mounted is fired when $nextTick changes, When testData changes in setTimeout it’s the third time. This means that it will be triggered whenever the bidirectional binding data it is listening for changes, so that it can listen for multiple data. Applicable scenarios include form verification, when all data is filled in and the button is lit, you can listen to more than one, and when multiple asynchronous returned data meet the conditions at the same time.
One last question