Kia King Ishii dbcbb112a7 docs: update the new `logger` plugin export path (#1784) 4 年之前
..
README.md 4fa68ab4f0 docs: unified the tips format (#1719) 5 年之前
actions.md 2ffedd6b27 docs: Minor typo fix for Actions docs page. (#1606) 5 年之前
forms.md 28284a5e3f fix docs description 5 年之前
getters.md 041df8da91 docs: added Scrimba lessons (#1512) 6 年之前
hot-reload.md 91e2f7cd29 docs: add Dynamic module hot reloading example (#1744) 5 年之前
modules.md 3bd49b71dc docs(guide/modules): Add examples for mapGetters with namespace (#1782) 4 年之前
mutations.md d4d04305e0 docs: update on object spread syntax (#1642) 5 年之前
plugins.md dbcbb112a7 docs: update the new `logger` plugin export path (#1784) 4 年之前
state.md d4d04305e0 docs: update on object spread syntax (#1642) 5 年之前
strict.md fab5054bc7 docs: reorganize file structure 7 年之前
structure.md fab5054bc7 docs: reorganize file structure 7 年之前
testing.md 7da1f139ba docs: fix false negative when checking `payload: false` in `testAction` (#1425) 5 年之前

README.md

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:

  1. Vuex stores are reactive. When Vue components retrieve state from it, they will reactively and efficiently update if the store's state changes.

  2. 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, and enables tooling that helps us better understand our applications.

The Simplest Store

:::tip NOTE We will be using ES2015 syntax for code examples for the rest of the docs. If you haven't picked it up, you should! :::

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

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    count: 0
  },
  mutations: {
    increment (state) {
      state.count++
    }
  }
})

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

store.commit('increment')

console.log(store.state.count) // -> 1

In order to have an access to this.$store 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 store option:

new Vue({
  el: '#app',
  store: store,
})

:::tip If you're using ES6, you can also go for ES6 object property shorthand notation (it's used when object key has the same name as the variable passed-in as a property):

new Vue({
  el: '#app',
  store
})

:::

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

methods: {
  increment() {
    this.$store.commit('increment')
    console.log(this.$store.state.count)
  }
}

Again, the reason we are committing a mutation instead of changing store.state.count 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 most basic Vuex counter app.

Next, we will discuss each core concept in much finer details, starting with State.