Skip to content

类型 ​

Pinia ​

Pinia 实例类型(createPinia 返回)。

成员签名说明
use(plugin: PiniaPlugin) => Pinia注册插件,链式
usePersistPlugin(options: PersistOptions) => Pinia桥接安全的持久化插件登记(链式)
install(app: any) => voidapp.use(pinia) 入口(仅 Web 端可靠,原生端请用 setActivePinia)

PiniaStoreBase ​

class 式 store 基类,所有 store 继承它获得 $-API 与持久化能力。

成员签名说明
$idstringstore id(defineStore 创建实例时注入)
$stateany | null当前 state 引用(bindState 后与子类 state 同引用)
bindState(state: any) => voidconstructor 中 super() 后调用一次,绑定响应式 state
$patch(partial: UTSJSONObject) => void批量修改(经 _hydrate 装载)并通知订阅器
$reset() => void调用 _doReset 钩子后通知订阅器
$save() => void显式保存点(direct 修改 state 后的落盘触发)
$subscribe(cb: StateChangeCallback) => Unsubscribe订阅状态变更($patch / $reset / $save 触发)
$onAction(cb: ActionChangeCallback) => Unsubscribe订阅 action 调用(经 callAction 包装的 action)
$dispose() => void注销实例,下次 useStore() 重建
callAction(name: string, fn: () => any) => any | null让 action 触发 $onAction 订阅
_doReset() => void钩子:定义 $reset 行为(按需重写)
_hydrate(data: UTSJSONObject) => void钩子:装载局部数据 / 持久化恢复(按需重写)
_serialize() => UTSJSONObject钩子:state 快照(按需重写)

PiniaPlugin ​

uts
export type PiniaPlugin = (ctx: PiniaPluginContext) => void

插件函数类型。UTS 约束:返回 void(不能返回对象合并进 store),插件逻辑通过 ctx 完成。

PiniaPluginContext ​

uts
export type PiniaPluginContext = {
  pinia: Pinia,
  store: any,
  storeId: string
}
字段说明
pinia当前 Pinia 实例
store刚创建的 store 实例(any,使用处 as any as PiniaStoreBase 收窄)
storeIdstore id

StateChangeCallback ​

uts
export type StateChangeCallback = (type: string, state: UTSJSONObject) => void

$subscribe 回调:type = 'patch' | 'reset' | 'save';state = _serialize() 快照。

ActionInfo / ActionChangeCallback ​

uts
export type ActionInfo = {
  name: string
}

export type ActionChangeCallback = (info: ActionInfo) => void

$onAction 回调数据:仅经 callAction / callActionWithArgs 包装的 action 被追踪。

Unsubscribe ​

uts
export type Unsubscribe = () => void

取消订阅函数($subscribe / $onAction 的返回值)。

StorageAdapter ​

uts
export type StorageAdapter = {
  get: ((key: string) => string | null) | null,
  set: ((key: string, value: string) => void) | null,
  remove: ((key: string) => void) | null
}

存储适配器(持久化后端):默认实现基于 uni storage(零依赖),可换 unix-storage / 原生 preferences。三个回调字段均可省略(null)——省略的回调回退默认 uni storage 行为;可空声明使桥接转换获得判空保护。

PersistOptions ​

uts
export type PersistOptions = {
  keyPrefix: string,
  includeStores: Array<string> | null,
  excludeStores: Array<string>,
  serializer: ((state: UTSJSONObject) => string) | null
}
字段说明
keyPrefix存储键前缀(完整键 = keyPrefix + storeId),默认 'ux-pinia:'
includeStores仅持久化指定 store(null = 全部)
excludeStores排除指定 store
serializer自定义序列化器(null = JSON 序列化)

UTS 类型完整

UTS 对象字面量必须完整:调用 createPersistPlugin 时四个字段都要显式给出(可选语义用 null 表达),参见基础用法示例。

store 类型约定 ​

store = PiniaStoreBase 子类 + 独立 state type:

uts
export type CounterState = {
  count: number
  name: string
}

export class CounterStore extends PiniaStoreBase {
  state: CounterState = reactive<CounterState>({ count: 0, name: 'pinia' })

  constructor() {
    super()
    this.bindState(this.state)
  }
}

Released under the MIT License.