Vuex is a State Management Pattern for VueJS. It helps to store data clean, separated, and globally accessible state-

Get Started. It's Free
or sign up with your email address
VUEX by Mind Map: VUEX

1. What is Vuex?

1.1. Vuex is a state management pattern + library for Vue.js applications. It serves as a centralized store for all the components in an application, with rules ensuring that the state can only be mutated in a predictable fashion.

1.2. Library for Vue JS

1.3. Inspired by

1.3.1. Flux

1.3.2. Redux

1.3.3. The Elm Architecture

2. What is State Management Pattern?

2.1. It is a self-contained app with the following parts:

2.1.1. State

2.1.2. View

2.1.3. Action

2.2. One way data flow is the simplest representation of this pattern.

2.3. this simplicity quickly breaks down when we have multiple components that share a common state:

2.4. What is the solution?

2.4.1. extract the shared state out of the components, and manage it in a global singleton

2.5. What will be the benefits?

2.5.1. more Structure

2.5.2. maintainability

2.5.3. view becomes big but all the components can access the state or trigger action

3. When should I use it?

3.1. When you are building a medium-to-large-scale SPA

4. Core Concepts

4.1. Store

4.1.1. A "store" is basically a container that holds your application state.

4.1.2. Center of every Vuex application

4.1.3. It is similar to a plain global object. Although it is more in these points. Vuex stores are reactive When Vue components retrieve state from it, they will reactively and efficiently update if the store's state changes. You cannot directly mutate the store's state. The only way to change a store's state is by explicitly committing mutations. This ensures every state change leaves a track-able record

4.2. State

4.2.1. Vuex State Uses Single State Tree Usually you will have only one store for each application Tree makes it straightforward to locate a specific piece of state Allows us to easily take snapshots of the current app state for debugging purposes The data you store in Vuex follows the same rules as the data in a Vue instance, ie the state object must be plain.

4.2.2. Get State To a Component We can retrive the State In Vue's Computed Section

4.3. Getters

4.3.1. When we need derived and computed state based on store state

4.3.2. They are like computed properties in VueJS.

4.4. Mutations

4.4.1. Mutation handler functions must be synchronous

4.4.2. This is the only way to actually change state in a Vuex store

4.4.3. We can do this by committing a mutation We can commit this way

4.4.4. Vuex mutations are very similar to events

4.4.5. Consists Handler function State as param of the function It can have the payload as the second param We make changes on the states in the Handler

4.4.6. Constant Name For Mutations This allows the code to take advantage of tooling like linters

4.5. Actions

4.5.1. Are similar to mutations

4.5.2. But they are for asynchronous operations

4.5.3. Action handlers receive a context Through the {context} property you access context.commit context.state context.getters context.dispatch

4.6. Modules

4.6.1. Single State Stree Store all information in one big Object

4.6.2. To avoid code bloat, we can use modules to separate our code

4.6.3. Vuex allows us to separate our code into Modules Each Module can have States Mutations Actions Getters

4.6.4. Namespacing By default, actions and mutations are still registered under the global namespace Setting the namespaced parameter true getters, actions and mutations will be automatically namespaced based on the path the module is registered at.