Skip to content

docs(zh): translate state subscription flush timing #2994

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: v3
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/docs/zh/core-concepts/state.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,19 @@ cartStore.$subscribe((mutation, state) => {
})
```

### 刷新时机 %{#flush-timing}%

在底层实现上,`$subscribe()` 使用了 Vue 的 `watch()` 函数。你可以传入与 `watch()` 相同的选项。当你想要在 **每次** state 变化后立即触发订阅时很有用:

```ts{4}
cartStore.$subscribe((state) => {
// 每当状态发生变化时,将整个 state 持久化到本地存储
localStorage.setItem('cart', JSON.stringify(state))
}, { flush: 'sync' })
```

### 取消订阅 %{#detaching-subscriptions}%

默认情况下,_state subscription_ 会被绑定到添加它们的组件上 (如果 store 在组件的 `setup()` 里面)。这意味着,当该组件被卸载时,它们将被自动删除。如果你想在组件卸载后依旧保留它们,请将 `{ detached: true }` 作为第二个参数,以将 _state subscription_ 从当前组件中*分离*:

```vue
Expand Down