The difference between the watch computed methods vue

A, computed and methods

computedAttribute is calculated, methodsmethod, can be implemented on a data processing and then output the data.
The difference is computedcalculated based on their properties are dependent caching. Calculation of property computedwill only be re-evaluated when it changed its dependencies. This means that the property will be returned immediately calculate the results before, without having to perform the function again as long as the data in the message data has not changed, many visits reversedMessage (handler for processing the message). As for the method, as long as the occurrence of re-rendering, method invocation will always perform this function.
When there is a performance overhead is relatively large computational properties of A, it needs to traverse a huge array and do a lot of calculations. Then we may have other computational properties depend on A, this time, we need to cache. That is, to use computedinstead methods. But for every property need to be recalculated, such as the return value of the following function function () { return Date.now() }, we had better use methods.
In short: large amount of data needs to be cached when used computed; indeed need to be reloaded each time, when the cache does not need to use methods.

Two, computed and watch

Computed Property name suggests is calculated by other variables to another property, fullName recalculate their values when it depends on firstName, lastName these two variables change.
Further, having a calculated attribute cache. Is calculated based on their properties depend caching. Calculation of property will only be re-evaluated when it changed its dependencies. This means that as long as the lastName and firstName are not changed, many visits to the calculation results before the property will return immediately fullName computing without having to perform functions again.
While the listener watchis listening to a specific value, a specific function is executed when the value change. E.g. tab assembly, we can monitor the current page, executing the corresponding function data acquired when the page changes.






 

Guess you like

Origin www.cnblogs.com/bamboopanders/p/12556144.html