Skip to content

Commit b913c3d

Browse files
authored
docs(zh): translate state subscription flush timing (#2994)
1 parent f354c24 commit b913c3d

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

packages/docs/zh/core-concepts/state.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,19 @@ cartStore.$subscribe((mutation, state) => {
252252
})
253253
```
254254

255+
### 刷新时机 %{#flush-timing}%
256+
257+
在底层实现上,`$subscribe()` 使用了 Vue 的 `watch()` 函数。你可以传入与 `watch()` 相同的选项。当你想要在 **每次** state 变化后立即触发订阅时很有用:
258+
259+
```ts{4}
260+
cartStore.$subscribe((state) => {
261+
// 每当状态发生变化时,将整个 state 持久化到本地存储
262+
localStorage.setItem('cart', JSON.stringify(state))
263+
}, { flush: 'sync' })
264+
```
265+
266+
### 取消订阅 %{#detaching-subscriptions}%
267+
255268
默认情况下,_state subscription_ 会被绑定到添加它们的组件上 (如果 store 在组件的 `setup()` 里面)。这意味着,当该组件被卸载时,它们将被自动删除。如果你想在组件卸载后依旧保留它们,请将 `{ detached: true }` 作为第二个参数,以将 _state subscription_ 从当前组件中*分离*
256269

257270
```vue

0 commit comments

Comments
 (0)