Мой магазин vuex
выглядит так, но при вызове addCustomer
я получаю 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
}
}
});
Это привязка / шаблон addCustomer
:
<template>
<button class="button" @click="addCustomer">Add Customer</button>
</template>
Это определение для addCustomer
:
<script>
export default {
name: "bootstrap",
methods: {
addCustomer: function() {
const customer = {
name: 'Some Name',
};
this.$store.commit('addCustomer', customer);
}
}
}
</script>
Всего 1 ответ
Вам не хватает state
в параметрах функции addCustomer: function (customer)
( 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
}
}
});