Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue: computed vs data(), for vuex bindings?

Tags:

vue.js

vuex

(Note: I not am not asking about how to use watch).

I have this form template and want to bind it to some variables, for example objectvalue3, that are tracked in a Vuex store (note: different forms may be present on one page, so props.formname tells the form where to look).

<template>
    <div>
    Tracking formname_:{{formname_}}:
        <form method="post" autocomplete="off">
            <input type="text" name="objectvalue3" :value="objectvalue3" />
            <input type="submit" class="btn btn-primary btn-success" value="Track" @click.prevent="write">        
        </form>
    </div>
</template>
....
props: {
    formname: {
        type: String,
        default: "main"
    }
},

Tracking it in data does not work - i.e. the form does not get updated - it just keeps the value the vuex was initialized to :

data: function() {
    return {
        formname_: this.formname
        ,objectvalue3: this.$store.state.tracker[this.formname].objectvalue3
},

But using computed works.

computed: {
    objectvalue3: function() {
        return this.$store.state.tracker[this.formname].objectvalue3
    }

I know I have to use computed when I need to do calculations. But there is not real calculation going on here. Unless, could it be the hash lookup via this.formname which tells the form which tracker attribute to look at that is causing a straight data to fail? Is this specific to vuex?

like image 877
JL Peyret Avatar asked May 08 '18 17:05

JL Peyret


People also ask

Why use computed property in Vuejs?

In Vue. js, computed properties enable you to create a property that can be used to modify, manipulate, and display data within your components in a readable and efficient manner. You can use computed properties to calculate and display values based on a value or set of values in the data model.

What is the main difference between a method and a computed value in VUE JS?

Instead of a computed property, we can define the same function as a method. For the end result, the two approaches are indeed exactly the same. However, the difference is that computed properties are cached based on their reactive dependencies.

What is the difference between a computed property and methods in VUE JS?

A computed property is cached, which implies that, for accessing data, the function runs just once if the value remains unchanged. Methods, on the other hand, need to run data properties checks every time, because they cannot tell otherwise if the values are changed inside the method property.

What is the difference between watchers and computed property in Vuejs?

Computed properties are used to calculate the value of a property based on some other conditions. Watchers, on the other hand, are not primarily used for changing the value of a property; instead, they are used to notify you when the value has changed and let you perform certain actions based on these changes.


1 Answers

Try this instead:

data: function() {
    return {
        formname_: this.formname,
        tracker: this.$store.state.tracker[this.formname]
},

Then:

<input type="text" name="objectvalue3" :value="tracker.objectvalue3" />

This should work because the data's tracker object is pointing to the one in the store, then whenever the value changes there, it'll also change in the tracker object.

computed works because it listens to changes in the variables used within it, while data doesn't because it applies the first value when executed and doesn't track changes.

like image 90
Phiter Avatar answered Oct 15 '22 17:10

Phiter