【问题标题】:ReferenceError state is not defined in vuex storeReferenceError 状态未在 vuex 存储中定义
【发布时间】:2019-03-21 08:43:22
【问题描述】:

我的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>

【问题讨论】:

    标签: javascript vue.js vuejs2 vue-component vuex


    【解决方案1】:

    您在 addCustomer 函数参数 (addCustomer: function (customer)) 中缺少 state

         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
             }
           }
         });
    

    【讨论】:

      猜你喜欢
      • 2020-06-21
      • 2020-11-21
      • 2018-08-09
      • 2018-09-22
      • 2019-09-09
      • 1970-01-01
      • 2020-06-21
      • 1970-01-01
      • 2021-07-25
      相关资源
      最近更新 更多