C
C
Cathari2020-10-01 20:56:00
Vue.js
Cathari, 2020-10-01 20:56:00

How to store user data using Vuex?

I'm making a Laravel + Vue + Vuex + API todo sheet. I want to put all the data about the user in the store. I don’t understand how to form a url (or rather, how to take the user’s id) in actions, here is the actual code:

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

let store = new Vuex.Store({
    state: {
        user:[],
    },
    mutations: {
        SET_USER_TO_STATE:(state, user) => {
            state.user = user;
        }
    },
    actions: {
        GET_USER({commit}) {
            return axios('/api/users/{user}', {   //вот url из роутов, но соответственно как сюда id авторизированного пользователя передать
                method: "GET"
            })
                .then((user) => {
                    return user;
                    commit('SET_USER_TO_STATE', user.data);
                })
                .catch(error => {
                console.log(error);
                    return error;
            })
        }
    },
    getters: {
        USER(state) {
            return state.user;
        }
    },

})

export default store;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WapSter, 2020-10-01
@Cathari

Pass payload as the second parameter, which will store the user id

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question