gutenbergdocs/docs/reference-guides/data/data-core-preferences.md
2025-10-22 01:40:18 +08:00

84 lines
2.0 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 偏好设置
命名空间:`core/preferences`
## 选择器
<!-- START TOKEN(Autogenerated selectors|../../../packages/preferences/src/store/selectors.js) -->
### get
返回一个布尔值,指示特定作用域中的偏好设置是否处于启用状态。
_参数_
- _state_ `Object`: 存储状态
- _scope_ `string`: 功能的作用域例如core/edit-post
- _name_ `string`: 功能名称
_返回值_
- `*`: 该功能是否启用?
<!-- END TOKEN(Autogenerated selectors|../../../packages/preferences/src/store/selectors.js) -->
## 操作
<!-- START TOKEN(Autogenerated actions|../../../packages/preferences/src/store/actions.js) -->
### set
返回用于指示应将偏好设置设定为特定值的操作对象。
_参数_
- _scope_ `string`: 偏好设置作用域例如core/edit-post
- _name_ `string`: 偏好设置名称
- _value_ `*`: 要设定的值
_返回值_
- `Object`: 操作对象
### setDefaults
返回用于指示应设定偏好设置默认值的操作对象。
_参数_
- _scope_ `string`: 偏好设置作用域例如core/edit-post
- _defaults_ `Object<string, *>`: 偏好设置名称与值的键值映射表
_返回值_
- `Object`: 操作对象
### setPersistenceLayer
设置持久化层。
当设置持久化层后,偏好设置存储将:
- 立即调用 `get` 方法,并将存储状态更新为返回的值
- 当偏好设置值发生变更时,调用 `set` 方法更新所有偏好设置
理想情况下,应在应用程序生命周期开始时、在向偏好设置存储分发任何其他操作之前分发 `setPersistenceLayer`
_参数_
- _persistenceLayer_ `WPPreferencesPersistenceLayer`: 持久化层
_返回值_
- `Object`: 操作对象
### toggle
返回用于指示应切换偏好设置状态的操作对象。
_参数_
- _scope_ `string`: 偏好设置作用域例如core/edit-post
- _name_ `string`: 偏好设置名称
<!-- END TOKEN(Autogenerated actions|../../../packages/preferences/src/store/actions.js) -->