Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vuejs: How to get computed property in created hook

Is it possible to get computed property value in created hook? My current implementation doesn’t work. My understanding is created hook would be called first which would call my async method and async method needs the computed property, but by the time computed property becomes available the created hook has already been executed with the “undefined” parameter.

Please suggest how can i make computed property available to created hook method.

created() {
        this.fetchPropertyOptions();
},
computed: {
    propertyList() {
        return this.data.value;
    },
},
methods: {
    async fetchPropertyOptions() {
        this.propertyOptionsMap = await api.GetOptions(this.propertyList);
    },
}
like image 211
zm10 Avatar asked Apr 25 '18 01:04

zm10


People also ask

How to calculate the computed properties in vuejs?

Since computed properties in VueJS are cached, there is no need for calculating unless there is a change in the items array. Vue 3 computed properties are reactive. Basically, the dependencies are reactive. So the execution of the below function will lead to a recalculation of the computed properties.

What is reactivity in VUE 3 computed properties?

Vue 3 computed properties are reactive. Basically, the dependencies are reactive. So the execution of the below function will lead to a recalculation of the computed properties. You can read more Vue 3 Reactivity in Composition API. Basically, we are pushing a new item to the array.

Can we use a computed property in the created method?

As a practical example, the codepen Joseph Silber linked in his answer shows the successful usage of a computed property in the created method. In fact, the only lifecycle method that can't use the injections & reactivity is beforeCreated. All other lifecycle methods, e.g. mounted, updated and even beforeDestroy & destroyed have access to it.

What is a computed property in JavaScript?

Think of a computed property as declaratively describing how to derive a value based on other values - its only responsibility should be computing and returning that value. Later in the guide we will discuss how we can perform side effects in reaction to state changes with watchers. The returned value from a computed property is derived state.


1 Answers

The Vue component creation goes through different lifecycles. There's an excellent diagram in the lifecycle diagram section of the documentation.

Reading the above mentioned diagram from top to bottom, you notice the created hook that you use in your example. It is called directly after Init injections & reactivity. This is where e.g. the props, data & computed are initialised. Right after that, the created lifecycle method is called.

As a practical example, the codepen Joseph Silber linked in his answer shows the successful usage of a computed property in the created method.

In fact, the only lifecycle method that can't use the injections & reactivity is beforeCreated. All other lifecycle methods, e.g. mounted, updated and even beforeDestroy & destroyed have access to it.

like image 110
Marc Dix Avatar answered Sep 28 '22 13:09

Marc Dix