【问题标题】:vuex: count is not defined in vue component filevuex:计数未在 vue 组件文件中定义
【发布时间】:2017-07-20 11:59:35
【问题描述】:

刚开始将 vuex 集成到我的 laravel 应用程序中,通过 vuex 官方文档中的示例计数器应用程序进行。

js/components/calculate.vue

<template>
  <div>
    <p>{{ count }}</p>
    <p>
      <button @click="increment">+</button>
      <button @click="decrement">-</button>
    </p>
  </div>
</template>
<script >
  import store from '../app.js'

  export default {
    computed: {
      count () {

        return store.state.count
      }
    },
    methods: {
      increment () {
        store.commit('increment')
      },
      decrement () {
        store.commit('decrement')
      }
    }
  }
</script>

js/app.js

const calculate = Vue.component('calculate', require('./components/calculate.vue'));

const store = new Vuex.Store({
  state: {
    count: 0
  },
  mutations: {
    increment: state => state.count++,
    decrement: state => state.count--
  }
});



const app = new Vue({
  el: '#app'
});

使用 webpack 构建

我在这行return store.count 中收到store.state.count 未定义错误。

【问题讨论】:

    标签: javascript laravel-5.3 vuejs2 vuex


    【解决方案1】:

    如果你在 app.js 文件中创建你的商店,(因为你没有提到)你需要添加

    const calculate = Vue.component('calculate', require('./components/calculate.vue'));
    
    Vue.use(Vuex) // this line
    
    const store = new Vuex.Store({
      state: {
        count: 0
      },
      mutations: {
        increment: state => state.count++,
        decrement: state => state.count--
      }
    });
    
    const app = new Vue({
      el: '#app'
    });
    

    不用说,你需要在这个文件中导入Vuex

    之后你需要修改这部分代码:

    const app = new Vue({
      el: '#app'
    });
    

    const app = new Vue({
      el: '#app',
      store // if you're vue-cli/babel else use store: store
    });
    

    而在 js/components/calculate.vue

    <template>
      <div>
        <p>{{ count }}</p>
        <p>
          <button @click="increment">+</button>
          <button @click="decrement">-</button>
        </p>
      </div>
    </template>
    <script >
     // import store from '../app.js' this import is not required
    
      export default {
        computed: {
          count () {
            return this.$store.state.count
          }
        },
        methods: {
          increment () {
            this.$store.commit('increment')
          },
          decrement () {
            this.$store.commit('decrement')
          }
        }
      }
    </script>
    

    不需要导入,因为 store 应该可以被 vue 实例的每个组件访问,要访问它,您只需要访问 vue 实例的 $store 属性即可在你的组件中是this (只要上下文没有通过回调或类似的方式改变。)

    【讨论】:

    • 当我添加 Vue.use(Vuex);获取Uncaught TypeError: plugin.apply is not a function
    • 那么不要添加vue.use(Vuex)部分。
    • 现在错误消失了,但在return store.state.count中显示另一个错误Uncaught TypeError: Cannot read property 'count' of undefined
    • @AmreshVenugopal 你有这个答案吗? stackoverflow.com/questions/42549580/…
    猜你喜欢
    • 2018-08-01
    • 2018-10-09
    • 2019-09-09
    • 2020-08-26
    • 1970-01-01
    • 2017-10-26
    • 2021-07-25
    • 2018-09-14
    • 2018-11-23
    相关资源
    最近更新 更多