Skip to content

Latest commit

 

History

History
99 lines (78 loc) · 1.94 KB

README.md

File metadata and controls

99 lines (78 loc) · 1.94 KB

Pinia Storage

npm license npm download GitHub top language GitHub issues npm package

Store Vue 3 Store data in the browser LocalStorage

Requirements

  • Vue.js ^3.0.0

Installation

npm

npm i @ctechhindi/pinia-storage

Usage

main.js

import piniaStorage from "@ctechhindi/pinia-storage";
const pinia = createPinia();
pinia.use(piniaStorage);

\stores\{sites}-store.js

import { defineStore } from "pinia";

export const useUsersStore = defineStore({
  id: "users-store",
  state: () => ({
    name: {
      first: "",
      last: "",
    },
  }),
  conserve: {
    enabled: true,
    strategies: [
      { storage: localStorage, states: ["name"] }, // Save custom state
      // { storage: localStorage, }, // Save all state
      // { storage: sessionStorage, states: ['name'] },
    ],
  },
  getters: {},
  actions: {},
});

Custom LocalStorage

\stores\{sites}-store.js

import { defineStore } from "pinia";

// Custom
const secureStorage = {
  setItem(key, state) {
    localStorage.setItem(key, state)
  },
  getItem(key) {
    localStorage.getItem(key, state)
  },
}

export const useUsersStore = defineStore({
  id: "users-store",
  state: () => ({
    name: {
      first: "",
      last: "",
    },
  }),
  conserve: {
    enabled: true,
    strategies: [
      { storage: secureStorage, states: ["name"] }, // Save custom state
    ],
  },
  getters: {},
  actions: {},
});

License

MIT License