npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@wlydfe/pinia-plus

v3.0.0

Published

npm template

Readme

@wlydfe/pinia-plus

NPM version License

一个增强版的 Pinia 状态管理解决方案,开箱即用,内置常用插件和实用功能。

✨ 特性

  • 🚀 开箱即用 - 预配置 Pinia 实例,无需额外设置
  • 💾 状态持久化 - 内置 pinia-plugin-persistedstate 插件
  • 🔄 Setup Store 重置 - 支持 Setup 语法下的 store 状态重置
  • 🗑️ 批量管理 - 提供批量重置和清理 store 的工具函数
  • 📦 Tree-shakable - 基于 ESM,支持按需导入
  • 🦾 完整类型支持 - 使用 TypeScript 编写,提供完整类型定义
  • 现代构建 - 使用 Unbuild 构建,输出优化的代码

📦 安装

# pnpm
pnpm add @wlydfe/pinia-plus pinia

# npm
npm install @wlydfe/pinia-plus pinia

# yarn
yarn add @wlydfe/pinia-plus pinia

🚀 快速开始

基础使用

// main.ts
import { createApp } from 'vue'
import { createPiniaPlus } from '@wlydfe/pinia-plus'
import App from './App.vue'

const app = createApp(App)
const pinia = createPiniaPlus()
app.use(pinia)
app.mount('#app')

定义 Store

// stores/user.ts
import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useUserStore = defineStore(
  'user',
  () => {
    const name = ref('')
    const age = ref(0)

    const setUser = (newName: string, newAge: number) => {
      name.value = newName
      age.value = newAge
    }

    return {
      name,
      age,
      setUser,
    }
  },
  {
    // 启用状态持久化
    persist: true,
  },
)

在组件中使用

<template>
  <div>
    <p>姓名: {{ name }}</p>
    <p>年龄: {{ age }}</p>
    <button @click="updateUser">更新用户</button>
    <button @click="resetUser">重置用户</button>
  </div>
</template>

<script setup lang="ts">
import { storeToRefs } from 'pinia'
import { useUserStore } from '@/stores/user'

const userStore = useUserStore()
const { name, age } = storeToRefs(userStore)

const updateUser = () => {
  userStore.setUser('张三', 25)
}

const resetUser = () => {
  // 使用内置的 $reset 方法重置状态(在 Setup 语法下也能正常工作)
  userStore.$reset()
}
</script>

🔧 API 参考

createPiniaPlus

import { createPiniaPlus } from '@wlydfe/pinia-plus'

const pinia = createPiniaPlus()

创建一个预配置的 Pinia 实例,已安装以下插件:

  • pinia-plugin-persistedstate - 状态持久化插件
  • resetPlugin - Setup Store 重置插件

其他导出

import { clearAllStoresState, setPiniaInstance } from '@wlydfe/pinia-plus'
  • clearAllStoresState - 彻底清理所有 store 的数据(内存状态 + 持久化存储)
  • setPiniaInstance - 设置 Pinia 实例供管理器使用(通常不需要手动调用,createPiniaPlus 会自动设置)

类型导出

import type { PiniaPlugin, PiniaPluginContext, PersistenceOptions } from '@wlydfe/pinia-plus'
  • PiniaPlugin - Pinia 插件类型
  • PiniaPluginContext - Pinia 插件上下文类型
  • PersistenceOptions - 持久化配置选项类型

注意:虽然 @wlydfe/pinia-plus 仍然提供默认导出的 pinia 实例以保持向后兼容,但推荐使用 createPiniaPlus() 函数来创建实例,这样可以更好地控制实例的生命周期。

🎯 内置插件

Reset 插件

为使用 Setup 语法定义的 store 提供 $reset 方法,可以将 store 状态重置为初始值。

import { defineStore } from 'pinia'
import { ref } from 'vue'

const useCountStore = defineStore('count', () => {
  const count = ref(0)
  const increment = () => count.value++

  return { count, increment }
})

// 在组件中使用
const countStore = useCountStore()
countStore.increment() // count: 1
countStore.$reset() // count: 0 (重置为初始值)

状态持久化

内置 pinia-plugin-persistedstate 插件,支持将 store 状态持久化到 localStorage。

import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useSettingsStore = defineStore(
  'settings',
  () => {
    const theme = ref('light')
    const language = ref('zh-CN')

    return { theme, language }
  },
  {
    persist: {
      key: 'app-settings',
      storage: sessionStorage, // 可选择存储方式
      paths: ['theme'], // 可选择持久化的字段
    },
  },
)

Store 批量管理

提供了批量管理所有 store 的工具函数,适用于退出登录等场景。

import { clearAllStoresState } from '@wlydfe/pinia-plus'

// 彻底清理所有 store 的数据(内存状态 + 持久化存储)
clearAllStoresState()

该函数会:

  1. 重置所有 store 的内存状态(调用每个 store 的 $reset 方法)
  2. 清理所有 store 的持久化存储(从 localStorage 和 sessionStorage 中删除)

📁 项目结构

@wlydfe/pinia-plus/
├── src/
│   ├── index.ts              # 主入口文件
│   ├── plugins/
│   │   └── reset-plugin.ts   # Reset 插件
│   ├── manager/
│   │   └── index.ts          # Store 管理器
│   └── utils/
│       └── clone-deep.ts     # 深拷贝工具函数
├── example/                  # 使用示例
├── dist/                     # 构建输出
└── README.md

🤝 贡献

欢迎提交 Issues 和 Pull Requests!

📄 许可证

MIT License © 2024-PRESENT wlydfe