Browse Source

docs: what is vuex

Evan You 8 years ago
parent
commit
04cced6797
5 changed files with 60 additions and 10 deletions
  1. 3 3
      docs/assets/vuex.ai
  2. BIN
      docs/en/images/flow.png
  3. BIN
      docs/en/images/vuex.png
  4. 57 7
      docs/en/intro.md
  5. BIN
      docs/en/vuex.png

File diff suppressed because it is too large
+ 3 - 3
docs/assets/vuex.ai


BIN
docs/en/images/flow.png


BIN
docs/en/images/vuex.png


+ 57 - 7
docs/en/intro.md

@@ -1,13 +1,63 @@
-## What is Vuex?
+# What is Vuex?
 
 
-Vuex is an application architecture for centralized state management in Vue.js applications. It is inspired by [Flux](https://facebook.github.io/flux/) and [Redux](https://github.com/rackt/redux), but with simplified concepts and an implementation that is designed specifically to take advantage of Vue.js' reactivity system.
+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. It also integrates with Vue's official [devtools extension](https://github.com/vuejs/vue-devtools) to provide advanced features such as zero-config time-travel debugging and state snapshot export / import.
 
 
-## Why do I need this?
+### What is a "State Management Pattern"?
 
 
-If your app is simple enough, you probably don't need Vuex. Don't apply it prematurely. But if you are building a medium-to-large-scale SPA, chances are you have run into situations that make you think about how to better structure things outside of your Vue components. This is where Vuex comes into play.
+Let's start with a simple Vue counter app:
 
 
-When using Vue.js alone, we often tend to store the state "inside" our components. That is, each component owns a piece of our application state, and as a result the state is scattered all over the place. However, sometimes a piece of state needs to be shared by multiple components. A commonly-seen practice is letting one component "send" some state to other components using the custom event system. The problem with this pattern is that the event flow inside large component trees can quickly become complex, and it's often difficult to reason about when something goes wrong.
+``` js
+new Vue({
+  // state
+  data () {
+    return {
+      count: 0
+    }
+  },
+  // view
+  template: `
+    <div>{{ count }}</div>
+  `,
+  // actions
+  methods: {
+    increment () {
+      this.count++
+    }
+  }
+})
+```
 
 
-To better deal with shared state in large applications, we need to differentiate between **component local state** and **application level state**. Application state does not belong to a specific component, but our components can still observe it for reactive DOM updates. By centralizing its management in a single place, we no longer need to pass events around, because everything that affects more than one component should belong there. In addition, this allows us to record and inspect every mutation for easier understanding of state changes, and even implement fancy stuff like time-travel debugging.
+It is a self-contained app with the following parts:
 
 
-Vuex also enforces some opinions on how to split state management logic into different places, but still allows enough flexibility for the actual code structure.
+- The **state**, which is the source of truth that drives our app;
+- The **view**, which is just a declarative mapping of the **state**;
+- The **actions**, which are the possible ways the state could change in reaction to user inputs from the **view**.
+
+This is an extremely simple representation of the concept of "one-way data flow":
+
+<p style="text-align: center; margin: 2em">
+  <img style="max-width:450px;" src="./images/flow.png">
+</p>
+
+However, the simplicity quickly breaks down when we have **multiple components that share common state**:
+
+- Multiple views may depend on the same piece of state.
+- Actions from different views may need to mutate the same piece of state.
+
+For problem one, passing props can be tedious for deeply nested components, and simply doesn't work for sibling components. For problem two, we often find ourselves resorting to solutions such as reaching for direct parent/child instance references or trying to mutate and synchronize multiple copies of the state via events. Both of these patterns are brittle and quickly leads to unmaintainable code.
+
+So why don't we extract the shared state out of the components, and manage it in a global singleton? With this, our component tree becomes a big "view", and any component can access the state or trigger actions, no matter where they are in the tree!
+
+In addition, by defining and separating the concepts involved in state management and enforcing certain rules, we also give our code more structure and maintainability.
+
+This is the basic idea behind Vuex, inspired by [Flux](https://facebook.github.io/flux/docs/overview.html), [Redux](http://redux.js.org/) and [The Elm Architecture](https://guide.elm-lang.org/architecture/). Unlike the other patterns, Vuex is also a library implementation tailored specifically for Vue.js to take advantage of its granular reactivity system for efficient updates.
+
+![vuex](./images/vuex.png)
+
+### When Should I Use It?
+
+Although Vuex helps us deal with shared state management, it also comes with the cost of more concepts and boilerplate. It's a trade-off between short term and long term productivity.
+
+If you've never built a large-scale SPA and jump right into Vuex, it may feel verbose and daunting. That's perfectly normal - if your app is simple, you will most likely be fine without Vuex. A simple [global event bus](http://vuejs.org/guide/components.html#Non-Parent-Child-Communication) may be all you need. But if you are building a medium-to-large-scale SPA, chances are you have run into situations that make you think about how to better handle state outside of your Vue components, and Vuex will be natural next step for you. There's a good quote from Dan Abramov, the author of Redux:
+
+> Flux libraries are like glasses: you’ll know when you need them.

BIN
docs/en/vuex.png


Some files were not shown because too many files changed in this diff