Skip to content

Commit 6b9295a

Browse files
committed
adjust docs structure
1 parent 04cced6 commit 6b9295a

File tree

10 files changed

+162
-239
lines changed

10 files changed

+162
-239
lines changed

docs/LANGS.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
* [English](en/)
1+
* [2.0 - English](en/)
2+
* [1.0 Docs](old/)

docs/en/SUMMARY.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
# Vuex
22

3-
### Large-scale State Management for Vue.js
4-
5-
> Note: This is docs for [email protected]. The docs for 1.x is [here](https://github.com/vuejs/vuex/tree/1.0/docs).
3+
> Note: This is docs for [email protected].
64
5+
- [Looking for 1.0 Docs?](https://github.com/vuejs/vuex/tree/1.0/docs)
76
- [Release Notes](https://github.com/vuejs/vuex/releases)
87
- [Installation](installation.md)
98
- [What is Vuex?](intro.md)
109
- [Getting Started](getting-started.md)
1110
- Core Concepts
1211
- [State](state.md)
12+
- [Getters](getters.md)
1313
- [Mutations](mutations.md)
1414
- [Actions](actions.md)
15-
- [Data Flow](data-flow.md)
15+
- [Modules](modules.md)
1616
- [Application Structure](structure.md)
1717
- [Plugins](plugins.md)
1818
- [Strict Mode](strict.md)

docs/en/data-flow.md

Lines changed: 0 additions & 89 deletions
This file was deleted.

docs/en/getters.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
2+
# Getters
3+
4+
Sometimes we may need to compute derived state based on store state, for example filtering through a list of items and counting them:
5+
6+
``` js
7+
computed: {
8+
doneTodosCount () {
9+
return this.$store.state.todos.filter(todo => todo.done).length
10+
}
11+
}
12+
```
13+
14+
If more than one component needs to make use of this, we have to either duplicate the function, or extract it into a shared helper and import it in multiple places - both are less than ideal.
15+
16+
Vuex allows us to define "getters" in the store (think of them as computed properties for stores):
17+
18+
``` js
19+
const store = new Vuex.Store({
20+
state: {
21+
todos: [
22+
{ id: 1, text: '...', done: true },
23+
{ id: 2, text: '...', done: false }
24+
]
25+
},
26+
getters: {
27+
doneTodosCount: state => {
28+
return state.todos.filter(todo => todo.done).length
29+
}
30+
}
31+
})
32+
```
33+
34+
The getters will be exposed on the `store.getters` object:
35+
36+
``` js
37+
store.getters.doneTodosCount // -> 1
38+
```
39+
40+
We can now easily make use of it inside any component:
41+
42+
``` js
43+
computed: {
44+
doneTodosCount () {
45+
return this.$store.getters.doneTodosCount
46+
}
47+
}
48+
```
49+
50+
### The `mapGetters` Helper
51+
52+
The `mapGetters` helper simply maps store getters to local computed properties:
53+
54+
``` js
55+
import { mapState } from 'vuex'
56+
57+
export default {
58+
// ...
59+
computed: {
60+
// mix the getters into computed with object spread operator
61+
...mapGetters([
62+
'doneTodosCount',
63+
'anotherGetter',
64+
// ...
65+
])
66+
}
67+
}
68+
```
69+
70+
If you want to map a getter to a different name, use an object:
71+
72+
``` js
73+
mapGetters({
74+
// map this.doneCount to store.getters.doneTodosCount
75+
doneCount: 'doneTodosCount'
76+
})
77+
```

docs/en/getting-started.md

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,50 +4,37 @@ At the center of every Vuex application is the **store**. A "store" is basically
44

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

7-
2. You cannot directly mutate the store's state. The only way to change a store's state is by explicitly dispatching **mutations**. This makes every state change easily track-able, and enables tooling that helps us better understand our applications.
7+
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.
88

99
### The Simplest Store
1010

11-
> **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](https://babeljs.io/docs/learn-es2015/)! The doc also assumes you are already familiar with the concepts discussed in [Building Large-Scale Apps with Vue.js](http://vuejs.org/guide/application.html).
11+
> **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](https://babeljs.io/docs/learn-es2015/)!
1212
13-
Creating a Vuex store is pretty straightforward - just provide an initial state object, and some mutations:
13+
After [installing](installation.md) Vuex, let's create a store. It is pretty straightforward - just provide an initial state object, and some mutations:
1414

1515
``` js
16-
import Vuex from 'vuex'
17-
18-
const state = {
19-
count: 0
20-
}
21-
22-
const mutations = {
23-
INCREMENT (state) {
24-
state.count++
16+
// Make sure to call Vue.use(Vuex) first if using a module system
17+
18+
const store = new Vuex.Store({
19+
state: {
20+
count: 0
21+
},
22+
mutations: {
23+
INCREMENT (state) {
24+
state.count++
25+
}
2526
}
26-
}
27-
28-
export default new Vuex.Store({
29-
state,
30-
mutations
3127
})
3228
```
3329

34-
Now, you can access the state object as `store.state`, and trigger a mutation by dispatching its name:
30+
Now, you can access the state object as `store.state`, and trigger a state change with the `store.commit` method:
3531

3632
``` js
37-
store.dispatch('INCREMENT')
33+
store.commit('INCREMENT')
3834

3935
console.log(store.state.count) // -> 1
4036
```
4137

42-
If you prefer object-style dispatching, you can also do this:
43-
44-
``` js
45-
// same effect as above
46-
store.dispatch({
47-
type: 'INCREMENT'
48-
})
49-
```
50-
51-
Again, the reason we are dispatching 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.
38+
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.
5239

53-
Now this is just the simplest possible example of what a store is. But Vuex is more than just the store. Next, we will discuss some core concepts in depth: [State](state.md), [Mutations](mutations.md) and [Actions](actions.md).
40+
Next, let's see [how to use the State inside Vue components](state.md).

docs/en/modules.md

Whitespace-only changes.

docs/en/mutations.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
Vuex mutations are essentially events: each mutation has a **name** and a **handler**. The handler function will receive the state as the first argument:
44

55
``` js
6-
import Vuex from 'vuex'
7-
86
const store = new Vuex.Store({
97
state: {
108
count: 1

0 commit comments

Comments
 (0)