我的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>
答案 0 :(得分:2)
您在addCustomer函数参数(state
)中缺少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
}
}
});