Skip to content
Advertisement

What is the best way to use $state in Vue.js?

I used to do AngularJS and now I wanna try to work with Vue.js.

But I have a simple question: what is the best way to use and access states? I saw so many solutions on Google but I can’t find which one is the best for a beginner.

I used to use states like this :

JavaScript

Can you give me an exemple of this code in Vue? Basically, go to homework and give him an homework.

Advertisement

Answer

In Vue.js it works with Vuex, the management library. You can find a documentation for state management in Vue.js here or and for Vuex here.

Example from the documentation for Vuex:

JavaScript

Use your Vuex store in your app:

JavaScript

Access your state in a component:

JavaScript

Change the state according to the example (state.count++):

JavaScript

Edit:

To complete the answer with a example for your question. Declare

JavaScript

Set a new state for homework:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement