ReferenceError 状态未在 vuex 存储中定义
我的 vuex
商店看起来像这样,但是当调用 addCustomer
我得到 ReferenceError: state is not defined
:
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
}
}
});
这是 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>
推荐答案
addCustomer 函数参数 (addCustomer: function (customer)
) 中缺少 state
:
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
}
}
});
相关文章