Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vuex mutations and actions not working

I'm new to Vue and Vuex and I'm trying to figure out how to mutate data in the store.

Currently in my State the default username is 'default' and I want to change it to 'otto', later ill grab this information from the database. But for the purpose of understanding I'm just trying to get this to work.

At this point the component is loaded correctly, and it displays 'default'. There are no errors present.

The store.js:

// store.js 
export default {
    state: {
        username: 'default'
    },
    getters: {
    },
    mutations: {
        changeUsername (state) {
            state.username = 'otto'
        }
    },
    actions: {
        changeUsername (context) {
            context.commit('changeUsername')
        }
    }
}

The vue file:

// Basic.vue
<template>
    <div>
        <p>{{ username }}</p>

    </div>
</template>

<script>
    import { mapState } from 'vuex';
    import { mapMutations } from 'vuex';
    import { mapActions } from 'vuex';


    export default {
        name: "basic",

        computed: {
            ...mapState([
                'username'
            ])
        },
        methods: {
            ...mapMutations([
                'changeUsername'
            ]),
            ...mapActions([
                'changeUsername'
            ])
        }
    }
</script>
like image 261
Otto Avatar asked Jun 21 '18 13:06

Otto


People also ask

Can I call action from mutation Vuex?

In Vuex, actions are functions that call mutations. Actions exist because mutations must be synchronous, whereas actions can be asynchronous. You can define actions by passing a POJO as the actions property to the Vuex store constructor as shown below. To "call" an action, you should use the Store#dispatch() function.

What is the difference between actions and mutations Vuex?

The Vuex docs say: Actions are similar to mutations, the differences being that: Instead of mutating the state, actions commit mutations. Actions can contain arbitrary asynchronous operations.

How do you call actions on Vuex?

You can call another Vuex action by passing the name of that action as a string as the first argument of dispatch : const store = new Vuex. Store({ actions: { walk(context) { context. dispatch("goForward"); }, goForward(context) { // }, }, });

Can Vuex actions be async?

An action in Vuex is where you perform interaction with APIs and commit mutations. Such interactions are inherently asynchronous.


1 Answers

Don't include your mutation, as it will be called by your action. And call the action on a button click for instance:

The store:

// store.js 
export default {
    state: {
        username: 'default'
    },
    getters: {
        username: state => state.username
    },
    mutations: {
        setUsername (state) {
            state.username = 'otto'
        }
    },
    actions: {
        updateUsername (context) {
            context.commit('setUsername ')
        }
    }
}

The component:

// Basic.vue
<template>
    <div>
        <p>{{ username }}</p>
        <button @click="updateUsername">Change!</button>
    </div>
</template>

<script>
    export default {
        name: "basic",

        computed: {
            username() {
                return this.$store.getters.username
            }
        },
        methods: {
            updateUsername() {
                this.$store.dispatch('updateUsername')
            }
        }
    }
</script>

Additionnal advice: be carefull when naming your mutations and actions. You don't want them to have the same name to avoid unwanted behaviors. I usually name my mutation setXXXX and my action getXXX or patchXXX depending of what the request does.

Working jsfiddle

like image 90
Finrod Avatar answered Sep 29 '22 16:09

Finrod