0

I have a simple Vue form component, it has some text inputs:

<input type="text" class="form-control" v-model="amount">

This text input has the following default value:

mounted() {
    this.$store.commit('refreshBalance')
  },

  data() {
      return {
        amount: this.$store.getters.baseBalance,
      };
  },

The problem with my code is that the value of amount is always the same, it doesn't change if i change the value of that Vuex store.

So, for example, if when i open the page this.$store.getters.baseBalance is equal to 10 and i change it to 20, the default value of that input field will always be 10. Is there any way i can make it reactive? Thanks in advance.

2
  • Where and how do you change the value of the baseBalance? Could you post that part of your code? Commented Dec 31, 2020 at 15:18
  • Yes, i added it. Basically when the component is loaded, i refresh that value Commented Dec 31, 2020 at 15:20

1 Answer 1

2

Change it to a computed property, and use the get and set methods. You’d also need to create an action in your store to update the baseBalance, which in the code below I’ve named SET_BALANCE.

When you update the input, the set method runs, which commits the value of amount to the store via. SET_BALANCE.

<input type="text" class="form-control" v-model="amount">

...

computed: {
  amount: {
    set(amount) {
      this.$store.commit('SET_BALANCE', { amount })
    },
    get() {
      return this.$store.getters.baseBalance
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

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.