0

I am enjoying learning Vue.js however I tend to keep running into the issue that when I set a data property based on state that it doesn't update the component if state changes.

For example...

Here are the snippets

<router-link v-if="!isDisabled" :to="{ path: '/effects' }">
   <button class="stepButton" :class="{ disabled: false }">Next</button>
</router-link>

<button v-else class="stepButton" :class="{ disabled: isDisabled }">Next</button>


data: function(){
            return {
                ailmentsLib: [
                    "Chronic Pain",
                    "Migraines",
                    "Muscle Spasms",
                    "Stress",
                    "Vertigo",
                    "Nausea"
                ],
                search: '',
                searchMatch: true,
                ailments: [
                    "Chronic Pain",
                    "Migraines",
                    "Muscle Spasms",
                    "Stress",
                    "Vertigo",
                    "Nausea"
                ],
                isDisabled: this.$store.state.ailment.length < 1 ? true : false
            }   
        }

I have the state as changing in my vue inspector but the button doesn't become enabled.

2 Answers 2

2

The problem is that data() does not react to changes in the store. You need to use a computed property to watch for state changes. Example:

computed: {
    isDisabled: function ()  {
        return this.$store.state.ailment.length < 1
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

to be more precise, data is reactive. but it's not set up to update automatically whenever $store changes
0

You can also use getters if you need to manipulate the data and reuse it. Getters Vuex

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.