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