[Vuejs]-Computed data in Vue component

1👍

For question 1 and 3 it’s better to ask the one who created to course. There is no real reason to do it like this. I wouldn’t do it the way he does, but indeed use created or mounted method.

For question 2:

In component’s data option must be a function, so that each instance
can maintain an independent copy of the returned data object::

https://v2.vuejs.org/v2/guide/components.html#data-Must-Be-a-Function

0👍

By Vue’s definition of created lifecycle hook: https://v2.vuejs.org/v2/api/#created

Called synchronously after the instance is created. At this stage, the
instance has finished processing the options which means the following
have been set up: data observation, computed properties, methods,
watch/event callbacks. However, the mounting phase has not been
started, and the $el property will not be available yet.

So generating data in data() will not trigger any related computed properties, watch/event callbacks. If you generate those data in created and your component has, for example, a watcher on times, there will be unneeded watch callbacks.

Btw, I am confused of the initialization of categories from this.$store.state.categories. Storing shared data from vuex in local data is unnecessary.

👤blaz

Leave a comment