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

@matiastang/pinia-persisted-state

v0.3.2

Published

pinia状态持久化

Readme

English | 中文

pinia-persisted-state

License: MIT

说明

pinia状态的本地持久化。

安装

  • pnpm导入
$ pnpm add @matiastang/pinia-persisted-state
  • yarn导入
$ yarn add @matiastang/pinia-persisted-state
  • npm
$ npm install @matiastang/pinia-persisted-state

配置

  • main.ts中如下便捷导入@matiastang/pinia-persisted-state
// pinia状态管理
import { createPinia } from 'pinia'
import { createPersistedState, persistedConfig } from '@matiastang/pinia-persisted-state'

const app = createApp(App)

// pinia
const pinia = createPinia()

// 便捷使用
pinia.use(createPersistedState)
// 查看默认配置
console.log(persistedConfig)

app.use(pinia)
  • main.ts中如下带配置导入@matiastang/pinia-persisted-state
// pinia状态管理
import { createPinia } from 'pinia'
import { createPersistedState, persistedConfig } from '@matiastang/pinia-persisted-state'

const app = createApp(App)

// pinia
const pinia = createPinia()

// 带配置使用
pinia.use(
    createPersistedState({
        key: 'pinia-key',
    })
)
// 查看配置
console.log(persistedConfig)

app.use(pinia)
  • persistedConfig@matiastang/pinia-persisted-state的配置。
  • persistedConfig.key是本地持久化key,默认值是pinia-key
  • persistedConfig.customKeycustom properties本地持久化key,默认值是pinia-custom-key
  • persistedConfig.customFilterKey是判定 store 成员是否缓存为custom properties的过滤函数,默认排除$_set前缀的属性。

使用

完成引入后,则pinia的所有状态及更新都将保存到storage中。 注意custom propertiesstate properties在赋值的时候才会同步到storage

  • 声明authUser Store,带有初始化值。
import { defineStore } from 'pinia'

interface State {
    name: string
    age: string
}

export const useAuthUserStore = defineStore('user', {
    state: (): State => ({
        name: 'name',
        age: 'age',
    }),
    actions: {
        setName(name: string) {
            this.name = name
        },
    },
})
  • 声明custom properties
import 'pinia'
import { Ref } from 'vue'

declare module 'pinia' {
    export interface PiniaCustomProperties {
        // by using a setter we can allow both strings and refs
        set userId(value: string | Ref<string>)
        get userId(): string

        // you can define simpler values too
        simpleNumber: number
    }
}
  • 声明state properties
import 'pinia'
import { Ref } from 'vue'

declare module 'pinia' {
    export interface PiniaCustomStateProperties<S> {
        set hello(value: string | Ref<string>)
        get hello(): string
    }
}
  • 使用并查看状态
import { useAuthUserStore } from '@/pinia/useAuthUserStore'
import { useTestStore } from '@/pinia/useTest'

const userStore = useAuthUserStore()
const testStore = useTestStore()
// 输出
console.log(
    userStore.simpleNumber,
    userStore.userId,
    userStore.$state.hello,
    testStore.simpleNumber,
    testStore.userId,
    testStore.$state.hello
)
  • 查看storage中保存的pinia-key数据如果配置了key则是对应的数据
{
    test: {data: "data"}
    user: {name: "name", age: "age"}
}

说明custom propertiesstate properties中只是申明。所有需要赋值之后才能看到数据。

userStore.userId = '001'
userStore.simpleNumber = 99
userStore.$state.hello = 'hello user'

testStore.userId = '002'
testStore.simpleNumber = 100
testStore.$state.hello = 'hello test'
{
    pinia-custom-key: {userId: "001", simpleNumber: 99}
    test: {data: "data", hello: "hello test"}
    user: {name: "name", age: "age", hello: "hello user"}
}
  • 可以看到userIdsimpleNumber这种custom properties使用userStoretestStore更新都是一样的,可以理解为pinia的全局变量。而hello这种共有state properties需要每个store自己控制。使用context.store.$state.hello可以修改所有storehello熟悉。因此可以自己写一个插件放到@matiastang/pinia-persisted-state该插件之后,全量初始或更新state properties中的数据。
const userID = ref('000001')
const hello = ref('hello pinia')
// 自定义基础插件,更新状态
export function myPiniaPlugin(context: PiniaPluginContext) {
    // 当然插件里面也可以处理custom properties
    context.store.userId = userID
    // 赋值
    context.store.$state.hello = hello
}
// pinia状态管理
import { createPinia } from 'pinia'
import { myPiniaPlugin } from '@/pinia/plugin'
import { createPersistedState, persistedConfig } from '@matiastang/pinia-persisted-state'

const app = createApp(App)

// pinia
const pinia = createPinia()

// 便捷使用
pinia.use(createPersistedState)
// 查看默认配置
console.log(persistedConfig)
// 有状态更新的插件
pinia.use(myPiniaPlugin)

app.use(pinia)

storage中的数据将更新。

{
    pinia-custom-key: {userId: "002", simpleNumber: 99}
    test: {data: "data", hello: "hello pinia"}
    user: {name: "name", age: "age", hello: "hello pinia"}
}

有点儿说多了,只需要知道@matiastang/pinia-persisted-state将持久化存储pinia中的数据就行。

测试

$ pnpm run typecheck        # 类型检查
$ pnpm test                 # 单元/集成测试
$ pnpm run test:coverage    # 覆盖率报告
$ pnpm run test:e2e         # 端到端测试(自动启动演示工程)

详细说明见 specs/001-test-suite/quickstart.md

版本

版本更新记录见 CHANGELOG.md

许可证

MIT © matiastang