Problem while trying to access data from vuex
P粉916760429
P粉916760429 2024-04-03 23:54:45
0
1
485

I tried putting the water data into Vuex and everything worked fine until I tried to access my data array.

const store = new Vuex.Store({
    state: {
        categories: {}
    },
    getters: {
        categories: (state) => {
            console.log(state.categories);
            return state.categories;
        }
    },
    mutations: {
        set_categories: (state, data) => {
            state.categories = data
        }
    },
    actions: {
        get_categories: (context) => {
            axios.get(`${baseUrl}/api/categories?pagination=0`)
                .then((response) => {
                    context.commit('set_categories', response.data);
                });
        }
    }
});

This is all the code in vista and this is how I access it in the vista component

mounted() {

        this.$store.dispatch('get_categories'); 
    },
computed: {
        stateCategories() { 
            return this.$store.state.categories 
        }
    },
console.log(this.stateCategories)

But my data does not appear. Do you know what the problem is? I tested with the vue tool developer and my data appears in vuex

P粉916760429
P粉916760429

reply all(1)
P粉333186285

Try to wait for response:

async mounted() {
    await this.$store.dispatch('get_categories'); 
},
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template