A
A
Alex2019-10-16 09:13:00
Vue.js
Alex, 2019-10-16 09:13:00

How to use localStorage in axios request in vuex?

Good afternoon, how to fix the configuration?

import Vue from 'vue'
import axios from 'axios'
import vuex from 'vuex'
Vue.use(vuex, axios);

export default new vuex.Store({
    state: {
        ParamsAffiliate: '',
    },
    mutations:{
        setParams(state, payload){
            state.ParamsAffiliate = payload;
        }
    },
    actions: {
        async getParams({commit, state}){
            let response = await axios.get('/information/userbar');
            if (response.status === 200) {
                commit('setParams', response.data);
            }
        }
    }
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Michael, 2019-10-16
@astrodeep

state: {
  ParamsAffiliate: JSON.parse(localStorage.getItem('params')),
},
mutations: {
  setParams(state, payload) {
    state.ParamsAffiliate = payload;
    localStorage.setItem('params', JSON.stringify(payload));
  }
},

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question