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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@jiuyue-/pinia-persisted-state

v1.0.9

Published

Persisted state for Pinia

Downloads

15

Readme

pinia-persisted-state

说明

pinia数据持久化

1. 安装

npm i @jiuyue-/pinia-persisted-state

2. 引入 pinia-persisted-state

  • 创建 pinia 并注册 pinia-persisted-state
// store/index.ts
import { createPinia } from 'pinia'

import createPersistedState from '@jiuyue-/pinia-persisted-state'

const pinia = createPinia()

// 1. 可以直接注册
// pinia.use(createPersistedState);

// 2. 也可以传入`PersistConfig`配置
pinia.use((context) =>
  createPersistedState(context, {
    // 存储数据的key
    name: 'custom-pinia',
    // 默认使用存储方式
    storage: sessionStorage,
  })
)
export default pinia
  • 在 main.ts 中注册 pinia
// main.ts
import { createApp } from 'vue'
import pinia from './store'

createApp(App).use(pinia).mount('#app')
  • 创建 store
    • 在 store 中添加persist属性,默认设置为true,表示开启本地持久化, 存储state中的所有数据。
    • 也可以设置为一个PersistOptions对象,结构如下:
      • key属性表示存储的key,默认是store.$id
      • storage属性表示存储的storage,默认是localStorage, 可以设置为sessionStorage
      • paths属性表示需要持久化的state中的属性,如果不设置或者为空数组,则持久化state中的所有属性。
// store/counter.ts
import { defineStore } from 'pinia'

export const useCounter = defineStore({
  id: 'counter',
  state: () => ({
    count: 0,
    a: 3,
    b: 4,
  }),
  // persist: true,
  persist: {
    key: 'counter',
    storage: localStorage,
    paths: ['count', 'a'],
  },
})

PersistConfig属性

type PersistConfig = {
  name?: string
  storage?: PersistStorage
}

| 属性名称 | 属性描述 | | -------- | ------------------------------------------------------------------------ | | name | 存储数据的key, 默认是pinia | | storage | 存储方式PersistStorage, 默认是localStorage, 可选项: sessionStorage |

PersistOptions属性

type PersistOptions = {
  key?: string
  storage?: PersistStorage
  paths?: string[]
}

| 属性名称 | 属性描述 | | -------- | -------------------------------------- | | key | 属性表示存储的key,默认是store.$id | | storage | 属性表示存储的storage,默认是localStorage | | paths | 属性表示需要持久化的state中的属性,如果不设置或者为空数组,则持久化state中的所有属性 |