I am not sure yet what the issue is but you should not have createPersistedState twice. It takes an array so you can just place PopUpForm in the first instance and both should work. Otherwise, make sure your state is read from the cookie after it is changed. You should use a getter to read that value in a computed method.
Nov 12, 2018
1
Level 8
Vuex persisted state not persisted
Hi I'm using vuex plugin along with js-cookie to make a simple cookie consent modal work, since I'm testing it what I do is make the cookiesAccepted cookie expire after 60 seconds, however after I accept cookies I refresh the tab and the cookie consent modal is there again.
What am I doing wrong?
This is my CookieModal component:
<template>
<transition name="cookie-fade">
<section class="cbanner_simp_maincontainer" v-if="cookiesAccepted == false">
<p class="cbanner_simp_paragraph">Esta página utiliza cookies propias para ofrecer una mejor experiencia en nuestra web.</p>
<button class="cbanner_simp_button" type="button" @click="acceptCookies()">
<span class="cbanner_simp_button_text">ACEPTAR</span>
</button>
</section>
</transition>
</template>
<!--SCRIPTS-->
<script>
import { mapState, mapActions } from 'vuex'
export default
{
name: 'CookieModal',
data: function () {
return {
}
},
computed: {
...mapState('CookieModal', ['cookiesAccepted']),
},
mounted () {
console.log(this.$options.name+' component successfully mounted');
},
methods:{
...mapActions('CookieModal', ['acceptCookies']),
}
}
</script>
This is my CookieModal vuex store module:
//STATE
const state = {
cookiesAccepted: false
}
//GETTERS
const getters = {
}
//ACTIONS
const actions = {
acceptCookies({commit}){
console.log('cookies_accepted');
commit('ACCEPT_COOKIES');
},
}
//MUTATIONS
const mutations = {
ACCEPT_COOKIES(state,) {
state.cookiesAccepted = true;
},
}
export default {
namespaced: true, state, getters, actions, mutations
}
As you can see, pretty simple.
Now in my index.js file inside store is where I use the vuex-persistedstate plugin along with js-cookie to make acceptedCookies = true.
/*
|-------------------------------------------------------------------------------
| VUEX store.js
|-------------------------------------------------------------------------------
*/
//Builds the data store from all of the modules for the app.
//Adds the promise polyfill for IE 11
//require('es6-promise').polyfill();
//Imports Vue and Vuex
import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'
import * as Cookies from 'js-cookie'
//Initializes Vuex on Vue as a plugin
Vue.use( Vuex )
//Imports all of the modules used in the application to build the data store.
import Auth from './modules/Auth.js';
import Reviews from './modules/Reviews.js';
import Globals from './modules/Globals.js';
import Bulletins from './modules/Bulletins.js';
import Contacts from './modules/Contacts.js';
import Bugs from './modules/Bugs.js';
import Schedules from './modules/Schedules.js';
import MainSlider from './modules/MainSlider.js';
import Posts from './modules/posts/Posts.js';
import PostBottombarList from './modules/posts/PostBottombarList.js';
import PostSidebarList from './modules/posts/PostSidebarList.js';
import PopUpModal from './modules/modals/PopUpModal.js';
import PopUpForm from './modules/modals/PopUpForm.js';
import MainModal from './modules/modals/MainModal.js';
import CookieModal from './modules/modals/CookieModal.js';
import AdminPanel from './modules/AdminPanel.js';
//Exports our data store.
export default new Vuex.Store({
strict: true, //debug
modules: {
Auth,
AdminPanel,
Globals,
Posts,
PostSidebarList,
PostBottombarList,
PopUpModal,
PopUpForm,
MainModal,
CookieModal,
Reviews,
Bulletins,
Contacts,
Bugs,
Schedules,
MainSlider,
},
plugins: [
createPersistedState({
paths: ['CookieModal'],
storage: {
getState: (key) => Cookies.getJSON(key),
setItem: (key, value) => Cookies.set(key, value, { expires: new Date(new Date().getTime() + 1 * 60 * 1000), secure: false }),
removeItem: key => Cookies.remove(key),
},
}),
createPersistedState({
paths: ['PopUpForm'],
storage: {
getState: (key) => Cookies.getJSON(key),
setItem: (key, value) => Cookies.set(key, value, { expires: new Date(new Date().getTime() + 1 * 60 * 1000), secure: false }),
removeItem: key => Cookies.remove(key),
},
}),
],
});
Please help I'm stuck here.
Please or to participate in this conversation.