robinvdvleuten / vuex-persistedstate

💾 Persist and rehydrate your Vuex state between page reloads.
https://npm.im/vuex-persistedstate
MIT License
5.77k stars 378 forks source link

Multiple keys support? #142

Closed abensur closed 6 years ago

abensur commented 6 years ago

Feature

Is it possible to make something like multiple keys?

import { name, version } from "../../package";
const versionKey = `${name}@${version}`
const appKey = `${name}`

export default ({store}) => {
  createPersistedState([{
      key: versionKey,
      paths: [
        'auth.token'
      ]
  }, {
      key: appKey,
      paths: [
        'client.ignore_list'
      ]
  }])(store)
}
robinvdvleuten commented 6 years ago

You could register the plugin multiple times with different keys / paths I guess?

abensur commented 6 years ago

Worked for me @robinvdvleuten !

import Vue from "vue";
import Vuex from "vuex";
import * as modules from "@/store/modules";
import appState from "vuex-persistedstate";
import versionState from "vuex-persistedstate";

import {
  name,
  version
} from "../../package";

Vue.use(Vuex);

const appKey = `${name}`;
const versionKey = `${name}@${version}`;

const store = new Vuex.Store({
  strict: true,
  modules,
  plugins: [
    versionState({
      key: versionKey,
      paths: [
        "auth",
        "auth.token",
        "auth.store_id",
        "auth.store_details"
      ]
    }),
    appState({
      key: appKey,
      paths: [
        "store.ignore_list",
        "store.customers_to_analyze"
      ]
    })
  ]
});

export default store;
robinvdvleuten commented 6 years ago

@abensur you don't have to import the plugin multiple times;

import Vue from "vue";
import Vuex from "vuex";
import * as modules from "@/store/modules";
import persistedState from "vuex-persistedstate";

import {
  name,
  version
} from "../../package";

Vue.use(Vuex);

const appKey = `${name}`;
const versionKey = `${name}@${version}`;

const store = new Vuex.Store({
  strict: true,
  modules,
  plugins: [
    persistedState({
      key: versionKey,
      paths: [
        "auth",
        "auth.token",
        "auth.store_id",
        "auth.store_details"
      ]
    }),
    persistedState({
      key: appKey,
      paths: [
        "store.ignore_list",
        "store.customers_to_analyze"
      ]
    })
  ]
});

export default store;
abensur commented 6 years ago

ahh silly me, thanks!