admin管理员组文章数量:1317906
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.$storemit('addCustomer', customer);
}
}
}
</script>
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.mit('addCustomer', customer);
}
}
}
</script>
Share
Improve this question
edited Jul 18, 2020 at 9:56
Boussadjra Brahim
1
asked Oct 16, 2018 at 18:27
Alexander ZeitlerAlexander Zeitler
13.1k14 gold badges89 silver badges142 bronze badges
1 Answer
Reset to default 7You'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
}
}
});
本文标签: javascriptReferenceError state is not defined in vuex storeStack Overflow
版权声明:本文标题:javascript - ReferenceError state is not defined in vuex store - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742032114a2416643.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论