Storage

与 useAppStorage 组合式函数相关的存储类型定义。

StorageType

表示浏览器存储类型的字符串字面量类型。

Source
export type StorageType = 'localStorage' | 'sessionStorage'
Example
const storage: StorageType = 'localStorage'

StorageConfig<T>

useAppStorage 函数的完整配置对象类型。

Source
export interface StorageConfig<T = unknown> {
  key: string
  defaultValue: T
  prefix: string
  storage: StorageType
}

属性说明

  • key: 存储项的唯一键名
  • defaultValue: 默认值
  • prefix: 键名前缀,用于创建命名空间
  • storage: 存储类型,localStoragesessionStorage

StorageConfigInput<T>

useAppStorage 函数的输入配置对象类型,prefixstorage 为可选参数。

Source
export type StorageConfigInput<T = unknown> = Partial<Omit<StorageConfig<T>, 'key' | 'defaultValue'>> & {
  key: string
  defaultValue: T
}
Example
import type { StorageConfigInput } from '@movk/core'
import { useAppStorage } from '@movk/core'

const config: StorageConfigInput<{ theme: string }> = {
  key: 'user-preferences',
  defaultValue: { theme: 'light' },
  storage: 'localStorage', // 可选
  prefix: 'app' // 可选
}

const storage = useAppStorage(config)

AppStorageReturn<T>

useAppStorage 函数的返回对象接口。

Source
export interface AppStorageReturn<T> {
  state: Ref<T>
  getItem: () => T
  setItem: (value: T) => void
  removeItem: () => void
}

属性说明

  • state: 响应式引用,与存储数据同步
  • getItem: 从存储中读取数据
  • setItem: 设置新值到存储
  • removeItem: 从存储中移除项
Example
import type { AppStorageReturn } from '@movk/core'
import { useAppStorage } from '@movk/core'

const storage: AppStorageReturn<string> = useAppStorage({
  key: 'my-key',
  defaultValue: 'hello'
})

// 使用响应式状态
console.log(storage.state.value) // 'hello'

// 更新值
storage.setItem('world')

// 读取值
const value = storage.getItem() // 'world'

// 移除项
storage.removeItem()

Changelog

7c832 — refactor!: 重构模块架构,拆分 utils 为专业化模块

7a568 — feat: 初始化项目结构与核心功能

Copyright © 2024 - 2026 YiXuan - MIT License