Skip to content

Commit fac84bc

Browse files
committed
Merge branch 'dev' into 4.0
2 parents 41098e1 + 6734ac5 commit fac84bc

File tree

14 files changed

+167
-117
lines changed

14 files changed

+167
-117
lines changed

docs/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
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.
66

7-
### What is a "State Management Pattern"?
7+
## What is a "State Management Pattern"?
88

99
Let's start with a simple Vue counter app:
1010

11-
``` js
11+
```js
1212
new Vue({
1313
// state
1414
data () {
@@ -58,7 +58,7 @@ If you want to learn Vuex in an interactive way you can check out this [Vuex cou
5858

5959
![vuex](/vuex.png)
6060

61-
### When Should I Use It?
61+
## When Should I Use It?
6262

6363
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.
6464

docs/api/README.md

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ sidebar: auto
66

77
## Vuex.Store
88

9-
``` js
9+
```js
1010
import Vuex from 'vuex'
1111

1212
const store = new Vuex.Store({ ...options })
@@ -36,7 +36,7 @@ const store = new Vuex.Store({ ...options })
3636

3737
Register actions on the store. The handler function receives a `context` object that exposes the following properties:
3838

39-
``` js
39+
```js
4040
{
4141
state, // same as `store.state`, or local state if in modules
4242
rootState, // same as `store.state`, only in modules
@@ -81,7 +81,7 @@ const store = new Vuex.Store({ ...options })
8181

8282
An object containing sub modules to be merged into the store, in the shape of:
8383

84-
``` js
84+
```js
8585
{
8686
key: {
8787
state,
@@ -122,7 +122,7 @@ const store = new Vuex.Store({ ...options })
122122
123123
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.
124124
125-
``` js
125+
```js
126126
{
127127
devtools: false
128128
}
@@ -176,22 +176,25 @@ const store = new Vuex.Store({ ...options })
176176
177177
- `subscribe(handler: Function, options?: Object): Function`
178178
179-
Subscribe to store mutations. The `handler` is called after every mutation and receives the mutation descriptor and post-mutation state as arguments:
179+
Subscribe to store mutations. The `handler` is called after every mutation and receives the mutation descriptor and post-mutation state as arguments.
180180
181-
``` js
182-
store.subscribe((mutation, state) => {
181+
```js
182+
const unsubscribe = store.subscribe((mutation, state) => {
183183
console.log(mutation.type)
184184
console.log(mutation.payload)
185185
})
186+
187+
// you may call unsubscribe to stop the subscription
188+
unsubscribe()
186189
```
187190
188191
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.
189192
190-
``` js
193+
```js
191194
store.subscribe(handler, { prepend: true })
192195
```
193196
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.
195198
196199
Most commonly used in plugins. [Details](../guide/plugins.md)
197200
@@ -201,28 +204,32 @@ const store = new Vuex.Store({ ...options })
201204
202205
> New in 2.5.0
203206
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.
205209
206-
``` js
207-
store.subscribeAction((action, state) => {
210+
```js
211+
const unsubscribe = store.subscribeAction((action, state) => {
208212
console.log(action.type)
209213
console.log(action.payload)
210214
})
215+
216+
// you may call unsubscribe to stop the subscription
217+
unsubscribe()
211218
```
212219
213220
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.
214221
215-
``` js
222+
```js
216223
store.subscribeAction(handler, { prepend: true })
217224
```
218225
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.
220227
221228
> New in 3.1.0
222229
223230
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*):
224231
225-
``` js
232+
```js
226233
store.subscribeAction({
227234
before: (action, state) => {
228235
console.log(`before action ${action.type}`)
@@ -237,7 +244,7 @@ const store = new Vuex.Store({ ...options })
237244
238245
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.
239246
240-
``` js
247+
```js
241248
store.subscribeAction({
242249
error: (action, state, error) => {
243250
console.log(`error action ${action.type}`)

docs/guide/README.md

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@ At the center of every Vuex application is the **store**. A "store" is basically
88

99
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.
1010

11-
### The Simplest Store
11+
## The Simplest Store
1212

1313
:::tip NOTE
1414
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/)!
1515
:::
1616

1717
After [installing](../installation.md) Vuex, let's create a store. It is pretty straightforward - just provide an initial state object, and some mutations:
1818

19-
``` js
19+
### Vuex 3.x (for Vue 2)
20+
21+
```js
2022
import Vue from 'vue'
2123
import Vuex from 'vuex'
2224

@@ -34,17 +36,35 @@ const store = new Vuex.Store({
3436
})
3537
```
3638

39+
### Vuex 4.x (for Vue 3)
40+
41+
```js
42+
import { createStore } from 'vuex'
43+
import { createApp } from 'vue'
44+
45+
const store = createStore({
46+
state () {
47+
return {
48+
count: 1
49+
}
50+
}
51+
})
52+
53+
const app = createApp({ /* your root component */ })
54+
app.use(store)
55+
```
56+
3757
Now, you can access the state object as `store.state`, and trigger a state change with the `store.commit` method:
3858

39-
``` js
59+
```js
4060
store.commit('increment')
4161

4262
console.log(store.state.count) // -> 1
4363
```
4464

4565
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:
4666

47-
``` js
67+
```js
4868
new Vue({
4969
el: '#app',
5070
store: store,
@@ -64,7 +84,7 @@ new Vue({
6484

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

67-
``` js
87+
```js
6888
methods: {
6989
increment() {
7090
this.$store.commit('increment')

docs/guide/actions.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ actions: {
3939
}
4040
```
4141

42-
### Dispatching Actions
42+
## Dispatching Actions
4343

4444
Actions are triggered with the `store.dispatch` method:
4545

@@ -98,7 +98,7 @@ actions: {
9898

9999
Note we are performing a flow of asynchronous operations, and recording the side effects (state mutations) of the action by committing them.
100100

101-
### Dispatching Actions in Components
101+
## Dispatching Actions in Components
102102

103103
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):
104104

@@ -121,7 +121,7 @@ export default {
121121
}
122122
```
123123

124-
### Composing Actions
124+
## Composing Actions
125125

126126
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?
127127

docs/guide/forms.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ The "Vuex way" to deal with it is binding the `<input>`'s value and call a metho
1515
``` html
1616
<input :value="message" @input="updateMessage">
1717
```
18+
1819
``` js
1920
// ...
2021
computed: {
@@ -40,13 +41,14 @@ mutations: {
4041
}
4142
```
4243

43-
### Two-way Computed Property
44+
## Two-way Computed Property
4445

4546
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:
4647

4748
``` html
4849
<input v-model="message">
4950
```
51+
5052
``` js
5153
// ...
5254
computed: {

docs/guide/getters.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const store = new Vuex.Store({
3434
})
3535
```
3636

37-
### Property-Style Access
37+
## Property-Style Access
3838

3939
The getters will be exposed on the `store.getters` object, and you access values as properties:
4040

@@ -69,7 +69,7 @@ computed: {
6969

7070
Note that getters accessed as properties are cached as part of Vue's reactivity system.
7171

72-
### Method-Style Access
72+
## Method-Style Access
7373

7474
You can also pass arguments to getters by returning a function. This is particularly useful when you want to query an array in the store:
7575

@@ -88,7 +88,7 @@ store.getters.getTodoById(2) // -> { id: 2, text: '...', done: false }
8888

8989
Note that getters accessed via methods will run each time you call them, and the result is not cached.
9090

91-
### The `mapGetters` Helper
91+
## The `mapGetters` Helper
9292

9393
The `mapGetters` helper simply maps store getters to local computed properties:
9494

0 commit comments

Comments
 (0)