Vue: Don't Watch
Use computed properties instead of watching your data.
Vue comes out of the box with a data binding watch mechanism, which is something you may be very familiar with if you happen to have an Angular 1 background.
However, much like the case with Angular, using watchers often gets overused and abused causing the developer to spend more time relying on DOM and binding changes to initiate reactive processes then properly relying on your users events.
This issue is nothing new, and the usage of copious watchers was already being chided even in the Angular community.
That being said, there are times when a watcher was necessary in Angular, usually in regards to a computed output requirement based on one or more bound data models.
Vue comes to the rescue with computed properties.
Computed properties allow outputting a bindable data variable that requires computation logic without the need of watchers.
See this simple example from the Vue documentation.
So remember to keep your watchers to an absolute minimum and rely on your user's actions and Vue's computed properties
Comments
Post a Comment