Skip to content

Commit 1c693b6

Browse files
authored
docs: add more example on unsubscribe method (#1050) (#1838)
fix #1050
1 parent 7cb9952 commit 1c693b6

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

docs/api/README.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,16 @@ 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
181181
``` js
182-
store.subscribe((mutation, state) => {
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.
@@ -191,7 +194,7 @@ const store = new Vuex.Store({ ...options })
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,13 +204,17 @@ 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
206210
``` js
207-
store.subscribeAction((action, state) => {
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.
@@ -216,7 +223,7 @@ const store = new Vuex.Store({ ...options })
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

0 commit comments

Comments
 (0)