Alexander Zeitler
Alexander Zeitler

Reputation: 13109

ReferenceError state is not defined in vuex store

My vuex store looks like this but when calling addCustomer I get ReferenceError: state is not defined:

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

Vue.use(Vuex);

export default new Vuex.Store({
  state: { customers: [] },
  mutations: {
    addCustomer: function (customer) {
      state.customers.push(customer); // error is thrown here
    }
  }
});

This is the addCustomer binding/template:

<template>
    <button class="button" @click="addCustomer">Add Customer</button>
</template>

This is the definition for addCustomer:

<script>
  export default {
    name: "bootstrap",
    methods: {
      addCustomer: function() {
        const customer = {
          name: 'Some Name',
        };

        this.$store.commit('addCustomer', customer);
      }
    }
  }
</script>

Upvotes: 3

Views: 9824

Answers (1)

Boussadjra Brahim
Boussadjra Brahim

Reputation: 1

You're missing the state in addCustomer function parameters (addCustomer: function (customer)) :

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

     Vue.use(Vuex);

     export default new Vuex.Store({
       state: { customers: [] },
       mutations: {
         addCustomer: function (state,customer) {
           state.customers.push(customer); // error is thrown here
         }
       }
     });

Upvotes: 7

Related Questions