ReferenceError 状态未在 vuex 存储中定义 [英] ReferenceError state is not defined in vuex store

查看:25
本文介绍了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
         }
       }
     });

这篇关于ReferenceError 状态未在 vuex 存储中定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆