0

I have a span showing an account balance. You can click on it to edit that balance. You can see below I'm applying the class hidden to the elements when the editing variable is changed.

<span @click="updateEditAccount(true, false)" v-bind:class="{ hidden: account.editing == true }">Balance ${{ account.balance }}</span>
<input v-bind:class="{ hidden: account.editing == false }" type="number" step="0.01" v-model="account.balance" >

This is the method that changes that value

updateEditAccount(editValue, updateLedger) {
    this.account.editing = editValue
    alert(this.account.editing)
    if(updateLedger) {

    }
},

When I set the initial value to true or false it displays the correct one. When the method is called I can see that the value is changed correctly.

The hidden class is simplye a display:none;

So the value does change, but the visible element doesn't change.

1 Answer 1

1

This is common reactive problem. You can use Vue.set

updateEditAccount(editValue, updateLedger) {
    this.$set(this.account, 'editing', editValue)
    if(updateLedger) {

    }
}

Updated:

Or you can use deep copy:

updateEditAccount(editValue, updateLedger) {
    this.account.editing = editValue
    this.account = JSON.parse(JSON.stringify(this.account))
    if(updateLedger) {

    }
}

ES6

updateEditAccount(editValue, updateLedger) {
    this.account = {...this.account, editing: editValue}
    if(updateLedger) {

    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

I made this changed, but have the same issue. Both ways change the value. If I check the value after it's updating, the model has the correct value, but the displayed element doesn't change.
I updated my answer. Can you try deep copy solution?
There is a case this.account.editing is updated but Vue component is not rendered again.
As my understanding, when you update this.account.editing, this.account is still point to old object and Vue doesn't recognize data change. It doesn't re-render HTML element. It only re-render if this.account object change.
You might want to try this.$forceUpdate(), but I'm not sure this work. I usually use deep copy with ES6 syntax.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.