@friendofsvelte/state

中文文档
0.2.0发布于 2026/02/10MITTypeScriptES Module零依赖近 30 天下载 3586

持续 Svelte 5 州、 地层和会话

中文使用文档
文档目录9 节
  1. 特征
  2. 安装
  3. 快速启动
  4. API 参考
  5. PersistentState<T>(key: string, initial?: T, storageType: S…
  6. 实例
  7. 基本用途
  8. 捐款
  9. 许可证

持久性斯维尔特5州

一个轻量级,类型安全的Svelte应用程序管理解决方案,具有内置存储持久性.

特征

  • 🎯 类型安全: 带有自动类型推论的完整类型脚本支持
  • 💾 持久性存储: 本地Storage 或会话中自动状态持续
  • 🪶 轻巧: 斯韦尔特以外的零依赖性
  • ⚡ 反应: 与斯维尔特反应系统的无缝集成
  • 🔄 自动同步: 自动同步各个组件的状态
  • 📦 简单 API: 只有一个功能来管理您的状态需求

安装

npm install @friendofsvelte/state

快速启动

  1. 使用 PersistentState数字 :
// new.svelte.ts / js
import { PersistentState } from '@friendofsvelte/state';

export const box = new PersistentState(
    'box',
    {
        color: '#ff3e00',
        dimensions: [100, 100]
    },
    'sessionStorage'
);
  1. 在组件中使用 :
<script lang="ts">
    import { box } from '$lib/new.svelte';

    const listColors = ['red', 'blue', 'green', 'yellow', 'purple', 'orange', 'pink', 'brown'];

    function switchNextColor() {
        const currentIndex = listColors.indexOf(box.current.color);
        const nextIndex = currentIndex + 1;
        if (nextIndex >= listColors.length) {
            box.current.color = listColors[0];
        } else {
            box.current.color = listColors[nextIndex];
        }
    }
</script>

<div
    style="background-color: {box.current
        .color}; width: 100px; height: 100px; color: gray; text-align: center;"
    class="m-2 rounded-2xl"
>
    {box.current.color}
</div>

<button onclick={switchNextColor} class="m-2 rounded-2xl bg-gray-700 px-3 text-gray-200">
    Change color
</button>

API 参考

PersistentState<T>(key: string, initial?: T, storageType: StorageType = 'localStorage')

创建或检索持久性状态容器.

参数:

  • key: 国家容器的独特标识符
  • initial: (可选) 初始状态值
  • storageType: (可选) 存储类型 - “ localStorage” 或“ sessionStorage” (默认: “localStorage” )

回返情况:

  • 类型反应状态对象 T

根据,富豪哈里斯' 本地存储测试

实例

基本用途

<script lang="ts">
    import { PersistentState } from '@friendofsvelte/state';

    const box = new PersistentState(
        'box',
        {
            color: '#ff3e00',
            dimensions: [100, 100]
        },
        'sessionStorage'
    );

    function switchNextColor() {
        const colors = ['red', 'blue', 'green', 'yellow', 'purple', 'orange', 'pink', 'brown'];
        const currentIndex = colors.indexOf(box.current.color);
        box.current.color = colors[(currentIndex + 1) % colors.length];
    }
</script>

<div
    style="background-color: {box.current
        .color}; width: 100px; height: 100px; color: gray; text-align: center;"
    class="m-2 rounded-2xl"
>
    {box.current.color}
</div>

<button onclick={switchNextColor} class="m-2 rounded-2xl bg-gray-700 px-3 text-gray-200">
    Change color
</button>

捐款

欢迎捐款! 请随意提交 拉请求。 。 。 .

许可证

麻省理工学院许可证 - 详情见LICENSE文件

版本与发布记录

@friendofsvelte/state 共发布 9 个版本,首次发布于 2025/02/15;过去 12 个月发布了 1 个版本。

版本发布日期距今
0.2.0latest2026/02/109 个月前
0.0.6-ts2025/02/251.7 年前
0.0.62025/02/251.7 年前
0.0.52025/02/251.7 年前
0.0.42025/02/241.7 年前
0.0.32025/02/241.7 年前

关于 @friendofsvelte/state 的常见问题

@friendofsvelte/state 是什么?

持续 Svelte 5 州、 地层和会话

如何安装 @friendofsvelte/state?

在项目目录运行 npm install @friendofsvelte/state,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:svelte(^5.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@friendofsvelte/state 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@friendofsvelte/state 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@friendofsvelte/state 可以商用吗?

@friendofsvelte/state 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@friendofsvelte/state 的下载量有多少?

根据 npm 官方下载统计,@friendofsvelte/state 在 2026/10/16 – 2026/11/14 这 30 天内被下载 3,586 次,日均约 120 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@friendofsvelte/state 还在维护吗?

最新版本 0.2.0 发布于 2026/02/10(9 个月前);至今共发布 9 个版本,其中过去 12 个月发布了 1 个;首次发布于 2025/02/15。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。