question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How can I only persist specific key in a Vuex Module

See original GitHub issue

Hello,

I have a question, How can I persist specific key in a module?:

reducer: (state) => ({
    adminAccountType: state.RolesAndPermissionModule.adminAccountType,
  })

I only want to persist the “adminAccountType” object within RolesAndPermissionModule , but when I do that it creates the new key in root state with “adminAccountType”

Thanks

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

35reactions
brookscacommented, Feb 21, 2019

aliraza,

Not sure if you got an answer to this or not. Looks like it was closed pretty quickly. I was having this same problem fixed it by doing the following:

// --- store/index.js

import Vue from 'vue'
import Vuex from 'vuex'
import VuexPersist from 'vuex-persist'

import persistedModuleName from './persistedModuleFile'

Vue.use(Vuex)
...

const vueLocalStorage = new VuexPersist({
  key: 'STORAGE_KEY',
  storage: window.localStorage,
  reducer: (state) => ({
    persistedModuleName: {
      CompanyName: state.persistedModuleName.CompanyName,
      CustomerId: state.persistedModuleName.CustomerId,
      SignedInUser: state.persistedModuleName.SignedInUser,
      SignedInUserRole: state.persistedModuleName.SignedInUserRole
    }
  })
})

export default new Vuex.Store({
  state: state,
  getters: getters,
  actions: actions,
  mutations: mutations,
  modules: {
    persistedModuleName
  },
  plugins: [vueLocalStorage.plugin]
})

Hope that helps.

4reactions
championswimmercommented, Feb 14, 2019

Reducer takes the state (in memory) as argument and returns the state (or its part) to be saved into your persistence.

Return whatever you want to return from reducer. Keeps keys you want. Drop keys you don’t want.

On Thu 14 Feb, 2019, 4:51 PM Malik Ali Raza <notifications@github.com wrote:

Hello,

I have a question, How can persist specific key in a module?:

reducer: (state) => ({ adminAccountType: state.RolesAndPermissionModule.adminAccountType, })

I’m only want to persist the “adminAccountType” object within RolesAndPermissionModule , but when I do that it creates the new key in root state with “adminAccountType”

Thanks

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/championswimmer/vuex-persist/issues/99, or mute the thread https://github.com/notifications/unsubscribe-auth/ABQ_yg5Yh8FqUAHceZD1RVamktvC3fHNks5vNUargaJpZM4a7YaR .

Read more comments on GitHub >

github_iconTop Results From Across the Web

Making only one module persistent with vuex-persistedstate
Looking at the API docs, you will need to configure the plugin to only persist a certain subset of the store. export default...
Read more >
Making only some state properties persistent inside ... - Graffino
How to make only some state properties of a Vuex module persistent using the createPersistedState and reducer function.
Read more >
Persist Vuex State with vuex-persist - DigitalOcean
The reducer option allows you to only store certain sections of the state. It's a simple function that gets passed the full state...
Read more >
vuex-persist - npm
A Typescript-ready Vuex plugin that enables you to save the state of your app to a persisted storage like Cookies or localStorage. Paypal...
Read more >
API Reference | Vuex
A module's state will be attached to the store's root state using the module's key. A module's mutations and getters will only receives...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found