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

pinia-plugin-piniastorage

v1.2.0

Published

pinia 的持久化插件,支持自定义存储方式

Downloads

40

Readme

介绍

pinia 的持久化插件,所有的都是可扩展的包括持久存储的方法

默认存储方式为 localStorage.setItem ,如果为移动端不支持,可以替换掉该方法

下载

npm i pinia-plugin-piniastorage

注册

import { createApp } from 'vue';
import App from './App.vue';
import { createPinia } from 'pinia';
import { piniaStorage } from "pinia-plugin-piniastorage";

const store = createPinia();
store.use(piniaStorage());
createApp(App).use(router).use(store).mount('#app');

基本使用

后面添加对象 storeOptions:{openStorage:true}

import { defineStore } from "pinia";
const useInfoStore = defineStore({
  id: "useInfo",
  state: () => ({
    test: "",
    token: "",
  }),
  getters: {},
  actions: {
    setTest(value: any) {
      this.test = value;
      this.token = "xxx";
    },
  },
  storeOptions: {
    openStorage: true, // 必须为 true 才会运行插件
    storageWay: {
      setStorageWay: (key, data) => sessionStorage.setItem(key, data), // 如这里将存储方法替换为了 sessionStorage
      getStorageWay: (key) => sessionStorage.getItem(key),
    },
    paths:['token'] // 只持久化 token
  },
});
export default useInfoStore;

全部配置

export interface Serializer {
  /**
   * 默认数据格式化的序列方法
   * @default JSON.stringify
   */
  serialize: (value: StateTree) => string;

  /**
   * 默认数据解析方法
   * @default JSON.parse
   */
  deserialize: (value: string) => StateTree;
}

export interface StorageWay {
  /**
   * 默认数据持久化的方法
   * @default localStorage.setItem
   */
  setStorageWay: (key: string, data: any) => void;

  /**
   * 默认数据持久化解析方法
   * @default localStorage.getItem
   */
  getStorageWay: (key: string) => any;
}

export interface YmStateOptions {
  /**
   * 是否开启本地存储
   * @default true
   */
  openStorage?: boolean;
  /**
   * 存储关键字
   * @default $store.id
   */
  key?: string;

  /**
   * 想要保存的部分路径
   * @default undefined
   */
  paths?: Array<string>;

  /**
   * 数据格式化的序列方法
   */
  serializer?: Serializer;
  /**
   * 持久化存储方法.
   */
  storageWay?: StorageWay;
  /**
   * 获取存储数据前触发钩子
   * @default undefined
   */
  beforeRestore?: (context: PiniaPluginContext) => void;

  /**
   * 获取存储数据后触发钩子
   * @default undefined
   */
  afterRestore?: (context: PiniaPluginContext) => void;
}