We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent f354c24 commit b913c3dCopy full SHA for b913c3d
packages/docs/zh/core-concepts/state.md
@@ -252,6 +252,19 @@ cartStore.$subscribe((mutation, state) => {
252
})
253
```
254
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
268
默认情况下,_state subscription_ 会被绑定到添加它们的组件上 (如果 store 在组件的 `setup()` 里面)。这意味着,当该组件被卸载时,它们将被自动删除。如果你想在组件卸载后依旧保留它们,请将 `{ detached: true }` 作为第二个参数,以将 _state subscription_ 从当前组件中*分离*:
269
270
```vue
0 commit comments