Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

133
Views
Can't use Vuex to display data

I am a new .net+vuejs learner and i'm using
this project template for vuejs

I'm trying to use vuex in my project to display countries data from database but it doesn't work

main.js

import 'bootstrap/dist/css/bootstrap.css'
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'

createApp(App).use(router).mount('#app')

index.js

import { createApp } from "vue";
import App from "./App.vue";
import Vuex from "vuex";
import countries from "./module/countries/countries";

createApp(App).use(Vuex);
export default new Vuex.Store({
modules: {
    countries: countries
}
});

store file

import axios from "axios";
const state = {
countries:[]
};


const countries = {
async getCountries({ commit }, payload) {
    const result = await axios.get("Pho/GetCountries");
    return result.data;
}
};

export default {
namespaced: true,
state,
mutations,
countries,
getters
}

my component

<script>
import axios from 'axios'
import swal from 'sweetalert';
export default {
   
    name: "Home",
    data() {
        return {
            countries: [],
            
        }
    },
    methods: {
        getCountries: async function () {
            this.countries = await this.$store.dispatch("countries/getCountries");
        }
        },
    mounted() {
        this.getCountries();
    }
    }
    </script>
   

ERROR: Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'dispatch') i tried to import store to my main.js like this

import 'bootstrap/dist/css/bootstrap.css'
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './index.js'

createApp(App).use(router, store).mount('#app')

but the app doesn't ever lauch

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It is not good practice to call APIs from storage files. Use vuex storage for only storage, use mixins(recommended) or component methods for sending API. I will show a simple approach to managing vuex storage! Storage File: index.js

import Vue from 'vue'
Vuex from 'vuex'
import router from '../router/index'
Vue.use(Vuex)
export const store = new Vuex.Store({
state: { 
    countries:[]
  },
  getters: {},
  mutations: {
      countriesChanger(state, payload) {
         state.countries = payload;
      },
  },
  actions: {},
});

Component File: MyComponent.vue

<template>
 <li v-for="country in countries">
   {{country}}
  </li>
</template>
<script>
export default {

name: "Home",
data() {
    return {
        countries: [],
        
    }
 },
 
  mounted() {
      this.getCountries(); // assume this function calls to your API
  },
  watch: {  
  "$store.state.countries": function () {
    // Listens when countries in storage updated
      this.countries = this.$store.state.countries;
    }
}
</script>

To change vuex storage item (to update countries) , use mutations after getting data from your API:

this.$store.commit("countriesChanger", countriesFromAPI);

That's it!

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!