A, computed and methods
computed
Attribute is calculated, methods
method, can be implemented on a data processing and then output the data.
The difference is computed
calculated based on their properties are dependent caching. Calculation of property computed
will 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 computed
instead 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 watch
is 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.