Hi everyone!
Could anyone explain me stores (Vuex or anyone) like I'm five? I don't understand.
Thanks!
Hi everyone!
Could anyone explain me stores (Vuex or anyone) like I'm five? I don't understand.
Thanks!
For further actions, you may consider blocking this person and/or reporting abuse
Michael Tharrington -
Ben Halpern -
Michael Tharrington -
Ben Halpern -
Once suspended, gerchog will not be able to comment or publish posts until their suspension is removed.
Once unsuspended, gerchog will be able to comment and publish posts again.
Once unpublished, all posts by gerchog will become hidden and only accessible to themselves.
If gerchog is not suspended, they can still re-publish their posts from their dashboard.
Once unpublished, this post will become invisible to the public and only accessible to Germán González.
They can still re-publish the post if they are not suspended.
Thanks for keeping DEV Community safe. Here is what you can do to flag gerchog:
Unflagging gerchog will restore default visibility to their posts.
Top comments (4)
A Vue application is a tree of components. Each component has 0 or 1 parents and 0 or more children.
In Vue, you usually send information from one component to another in a straight line, up or down.
You use props to pass data down the component tree, and events to pass it back up.
This isn't so great if you've got a really big tree. Passing hundreds of notes back and forth gets to be a real pain.
Another option is to put all your data in JavaScript singletons, like a bunch of cardboard boxes that your application can pass around to whoever needs it.
This isn't a bad solution, necessarily, but what if it could be better?
Vuex is like a train where each car has the same shape.
The first train car, the locomotive, has all the data your components need to share. Whenever you want to change that data, you commit an action or a mutation. This is like adding another car to the train. It has the same shape as the locomotive, just different data inside.
Why is this better than cardboard boxes? Because if something goes wrong while you're watching, you can pause the train or put it in reverse. You can inspect two cars and see what's different between them. You can debug the entire train instead of just one car.
Vuex also works really well with the Vue Devtools, an official browser extension, so you can easily watch what actions/mutations are happening and how the data is changing.
So Vuex makes it super easy to organize data, share it between components, and observe when it changes. How do you use it?
A Vuex store is a blueprint for one of those train cars. It looks like this:
And you can register it like this:
Then in a Vue component, in any method or lifecycle hook, you can do this:
Voila! Now
dataToShare
is equal to 1. If you're debugging and need to rewind it to 0, you can do so using the Vue Devtools.To access
dataToShare
in any component, you can do this:Usually you would do this in a
computed
method, so that every timedataToShare
changes, your component gets updated. This is another advantage of Vuex: it hooks into Vue's reactivity system, so when data updates, your application updates too.That's pretty much it! Any questions?
What a great answer!!! Thank you very much!.
Sorry for my english, I try to write the best I can.
I have a question. Maybe you just answered and I don't realized.
What's the difference between a store and a global service that I can access and get data? (Is the cardboard example?)
Thanks!
Your English is excellent.
Assuming the Vuex store and the global service are both local (no HTTP requests or anything), the differences are:
Excellent answer. Very clear for me!
Thank you very very much!