代码之家  ›  专栏  ›  技术社区  ›  Alexander Zeitler

vuex存储中未定义ReferenceError状态

  •  1
  • Alexander Zeitler  · 技术社区  · 6 年前

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

    添加客户

    <template>
        <button class="button" @click="addCustomer">Add Customer</button>
    </template>
    

    这就是 添加客户 :

    <script>
      export default {
        name: "bootstrap",
        methods: {
          addCustomer: function() {
            const customer = {
              name: 'Some Name',
            };
    
            this.$store.commit('addCustomer', customer);
          }
        }
      }
    </script>
    
    1 回复  |  直到 4 年前
        1
  •  6
  •   Boussadjra Brahim    6 年前

    你错过了比赛 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
             }
           }
         });