0

Just started to integrate vuex to my laravel application, going through with the sample counter application in the official vuex docs.

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

Using webpack build

I got store.state.count not defined error in this line return store.count.

1 Answer 1

0

If you are creating your store in app.js file, (since you haven't mentioned) you need to add

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

This goes without saying that you need to import Vuex in this file.

after which you need to modify, this part of your code:

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

to

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

And in 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>

The import is not required as the store is supposed to be accessible to each component of the vue instance, to access it, you just need to have access to $store property of the vue instance which in your component is this (as long as the context doesn't change by means of a callback or something similar.)

Sign up to request clarification or add additional context in comments.

4 Comments

When i add Vue.use(Vuex); get Uncaught TypeError: plugin.apply is not a function
Then don't add the vue.use(Vuex) part.
Now the error is gone but showing another error Uncaught TypeError: Cannot read property 'count' of undefined in return store.state.count
@AmreshVenugopal do you have an answer for this?? stackoverflow.com/questions/42549580/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.