You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/README.md
+3-3Lines changed: 3 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -4,11 +4,11 @@
4
4
5
5
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.
6
6
7
-
###What is a "State Management Pattern"?
7
+
## What is a "State Management Pattern"?
8
8
9
9
Let's start with a simple Vue counter app:
10
10
11
-
```js
11
+
```js
12
12
newVue({
13
13
// state
14
14
data () {
@@ -58,7 +58,7 @@ If you want to learn Vuex in an interactive way you can check out this [Vuex cou
58
58
59
59

60
60
61
-
###When Should I Use It?
61
+
## When Should I Use It?
62
62
63
63
Vuex helps us deal with shared state management with the cost of more concepts and boilerplate. It's a trade-off between short term and long term productivity.
Copy file name to clipboardExpand all lines: docs/api/README.md
+23-16Lines changed: 23 additions & 16 deletions
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ sidebar: auto
6
6
7
7
## Vuex.Store
8
8
9
-
```js
9
+
```js
10
10
importVuexfrom'vuex'
11
11
12
12
conststore=newVuex.Store({ ...options })
@@ -36,7 +36,7 @@ const store = new Vuex.Store({ ...options })
36
36
37
37
Register actions on the store. The handler function receives a `context` object that exposes the following properties:
38
38
39
-
```js
39
+
```js
40
40
{
41
41
state, // same as `store.state`, or local state if in modules
42
42
rootState, // same as `store.state`, only in modules
@@ -81,7 +81,7 @@ const store = new Vuex.Store({ ...options })
81
81
82
82
An object containing sub modules to be merged into the store, in the shape of:
83
83
84
-
```js
84
+
```js
85
85
{
86
86
key: {
87
87
state,
@@ -122,7 +122,7 @@ const store = new Vuex.Store({ ...options })
122
122
123
123
Turn the devtools on or off for a particular vuex instance. For instance passing false tells the Vuex store to not subscribe to devtools plugin. Useful for if you have multiple stores on a single page.
124
124
125
-
```js
125
+
```js
126
126
{
127
127
devtools:false
128
128
}
@@ -176,22 +176,25 @@ const store = new Vuex.Store({ ...options })
// you may call unsubscribe to stop the subscription
188
+
unsubscribe()
186
189
```
187
190
188
191
By default, new handler is added to the end of the chain, so it will be executed after other handlers that were added before. This can be overridden by adding `prepend:true` to `options`, which will add the handler to the beginning of the chain.
189
192
190
-
```js
193
+
```js
191
194
store.subscribe(handler, { prepend:true })
192
195
```
193
196
194
-
To stop subscribing, call the returned unsubscribe function.
197
+
The `subscribe` method will return an `unsubscribe` function, which should be called when the subscription is no longer needed. For example, you might subscribe to a Vuex Module and unsubscribe when you unregister the module. Or you might call `subscribe` from inside a Vue Component and then destroy the component later. In these cases, you should remember to unsubscribe the subscription manually.
195
198
196
199
Most commonly used in plugins. [Details](../guide/plugins.md)
197
200
@@ -201,28 +204,32 @@ const store = new Vuex.Store({ ...options })
201
204
202
205
> New in 2.5.0
203
206
204
-
Subscribe to store actions. The `handler` is called for every dispatched action and receives the action descriptor and current store state as arguments:
207
+
Subscribe to store actions. The `handler` is called for every dispatched action and receives the action descriptor and current store state as arguments.
208
+
The `subscribe` method will return an `unsubscribe` function, which should be called when the subscription is no longer needed. For example, when unregistering a Vuex module or before destroying a Vue component.
// you may call unsubscribe to stop the subscription
217
+
unsubscribe()
211
218
```
212
219
213
220
By default, new handler is added to the end of the chain, so it will be executed after other handlers that were added before. This can be overridden by adding `prepend:true` to `options`, which will add the handler to the beginning of the chain.
214
221
215
-
```js
222
+
```js
216
223
store.subscribeAction(handler, { prepend:true })
217
224
```
218
225
219
-
To stop subscribing, call the returned unsubscribe function.
226
+
The `subscribeAction` method will return an `unsubscribe` function, which should be called when the subscription is no longer needed. For example, you might subscribe to a Vuex Module and unsubscribe when you unregister the module. Or you might call `subscribeAction` from inside a Vue Component and then destroy the component later. In these cases, you should remember to unsubscribe the subscription manually.
220
227
221
228
> New in 3.1.0
222
229
223
230
Since 3.1.0, `subscribeAction` can also specify whether the subscribe handler should be called *before* or *after* an action dispatch (the default behavior is *before*):
224
231
225
-
```js
232
+
```js
226
233
store.subscribeAction({
227
234
before: (action, state) => {
228
235
console.log(`before action ${action.type}`)
@@ -237,7 +244,7 @@ const store = new Vuex.Store({ ...options })
237
244
238
245
Since 3.4.0, `subscribeAction` can also specify an `error` handler to catch an error thrown when an action is dispatched. The function will receive an `error` object as the third argument.
Copy file name to clipboardExpand all lines: docs/guide/README.md
+25-5Lines changed: 25 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -8,15 +8,17 @@ At the center of every Vuex application is the **store**. A "store" is basically
8
8
9
9
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.
10
10
11
-
###The Simplest Store
11
+
## The Simplest Store
12
12
13
13
:::tip NOTE
14
14
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/)!
15
15
:::
16
16
17
17
After [installing](../installation.md) Vuex, let's create a store. It is pretty straightforward - just provide an initial state object, and some mutations:
18
18
19
-
```js
19
+
### Vuex 3.x (for Vue 2)
20
+
21
+
```js
20
22
importVuefrom'vue'
21
23
importVuexfrom'vuex'
22
24
@@ -34,17 +36,35 @@ const store = new Vuex.Store({
34
36
})
35
37
```
36
38
39
+
### Vuex 4.x (for Vue 3)
40
+
41
+
```js
42
+
import { createStore } from'vuex'
43
+
import { createApp } from'vue'
44
+
45
+
conststore=createStore({
46
+
state () {
47
+
return {
48
+
count:1
49
+
}
50
+
}
51
+
})
52
+
53
+
constapp=createApp({ /* your root component */ })
54
+
app.use(store)
55
+
```
56
+
37
57
Now, you can access the state object as `store.state`, and trigger a state change with the `store.commit` method:
38
58
39
-
```js
59
+
```js
40
60
store.commit('increment')
41
61
42
62
console.log(store.state.count) // -> 1
43
63
```
44
64
45
65
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:
46
66
47
-
```js
67
+
```js
48
68
newVue({
49
69
el:'#app',
50
70
store: store,
@@ -64,7 +84,7 @@ new Vue({
64
84
65
85
Now we can commit a mutation from component's method:
Copy file name to clipboardExpand all lines: docs/guide/actions.md
+3-3Lines changed: 3 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -39,7 +39,7 @@ actions: {
39
39
}
40
40
```
41
41
42
-
###Dispatching Actions
42
+
## Dispatching Actions
43
43
44
44
Actions are triggered with the `store.dispatch` method:
45
45
@@ -98,7 +98,7 @@ actions: {
98
98
99
99
Note we are performing a flow of asynchronous operations, and recording the side effects (state mutations) of the action by committing them.
100
100
101
-
###Dispatching Actions in Components
101
+
## Dispatching Actions in Components
102
102
103
103
You can dispatch actions in components with `this.$store.dispatch('xxx')`, or use the `mapActions` helper which maps component methods to `store.dispatch` calls (requires root `store` injection):
104
104
@@ -121,7 +121,7 @@ export default {
121
121
}
122
122
```
123
123
124
-
###Composing Actions
124
+
## Composing Actions
125
125
126
126
Actions are often asynchronous, so how do we know when an action is done? And more importantly, how can we compose multiple actions together to handle more complex async flows?
Copy file name to clipboardExpand all lines: docs/guide/forms.md
+3-1Lines changed: 3 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -15,6 +15,7 @@ The "Vuex way" to deal with it is binding the `<input>`'s value and call a metho
15
15
```html
16
16
<input:value="message"@input="updateMessage">
17
17
```
18
+
18
19
```js
19
20
// ...
20
21
computed: {
@@ -40,13 +41,14 @@ mutations: {
40
41
}
41
42
```
42
43
43
-
###Two-way Computed Property
44
+
## Two-way Computed Property
44
45
45
46
Admittedly, the above is quite a bit more verbose than `v-model` + local state, and we lose some of the useful features from `v-model` as well. An alternative approach is using a two-way computed property with a setter:
0 commit comments