Getting Started

    At the center of every Vuex application is the store. A “store” is basically a container that holds your application state. There are two things that make a Vuex store different from a plain global object:

    NOTE

    After installing Vuex, let’s create a store. It is pretty straightforward - just provide an initial state object, and some mutations:

    Now, you can access the state object as , and trigger a state change with the method:

    In order to have an access to property in your Vue components, you need to provide the created store to Vue instance. Vuex has a mechanism to “inject” the store into all child components from the root component with the option:

    TIP

    Now we can commit a mutation from component’s method:

    Again, the reason we are committing a mutation instead of changing directly, is because we want to explicitly track it. This simple convention makes your intention more explicit, so that you can reason about state changes in your app better when reading the code. In addition, this gives us the opportunity to implement tools that can log every mutation, take state snapshots, or even perform time travel debugging.

    Using store state in a component simply involves returning the state within a computed property, because the store state is reactive. Triggering changes simply means committing mutations in component methods.

    Here’s an example of the .