0

I have a problem when I call action for mutation in method drawMap(). So, I registered my store component in main.js

import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'

Vue.config.productionTip = false

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')

And below my component where I call to action inside another method DRAWMAP, but I get the error

'Cannot read property 'dispatch' of undefined VueJS '

. What do I wrong?

   <script>
window.jQuery = require('jquery');
var $ = window.jQuery;
require('jvectormap');
require('../lib/jquery.vmap');
require('../lib/jquery.vmap.ukraine');
import {mapGetters} from 'vuex';

export default {
    data() {
        return {
            currentRegion: 'Львівська область',
            counter: 0
        }
    },
    mounted() {
        this.drawMap();
        this.$store.dispatch('SET_REGION', this.currentRegion);
    },
    computed: {
        ...mapGetters(['CHOOSED_REGION']),
    },  
    methods: {
        drawMap(){
            $('#vmap').vectorMap({
                map: 'ukraine_ua',
                onRegionClick: function(element, code, region)
                {
                    this.currentRegion = region;
                    this.$store.dispatch('SET_REGION', this.currentRegion);
                }
            });
        },
    }
}
</script>
2
  • Please add your code as text and not an image. Commented Dec 9, 2019 at 20:51
  • @zero298, changed Commented Dec 9, 2019 at 20:57

1 Answer 1

1

Try changing onRegionClick to use arrow function so that this.$store.dispatch makes reference to the top-level object.

onRegionClick: (element, code, region) => {
  this.currentRegion = region;
  this.$store.dispatch('SET_REGION', this.currentRegion);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks very much, you are genius @Gloria Morales Londoño!

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.