Sock Monkey
Sock Monkey

Reputation: 333

Mutating state in vuex

I'm trying to make an api call to my DB and commit the mutation but i'm not having much success.

Store.js

import Vue from 'vue'
import Vuex from 'vuex' 
import axios from 'axios';

Vue.use(Vuex) 

export const store = new Vuex.Store({  
   state: {
      photons: [],

    },
    mutations: {
      setData(state, value) {
        console.log(value)
        state.photons = value
      }
    },
    actions: {
      async getData(context) {
        const data = await axios.get('10.10.10.1:3000/DB')
        console.log(data)
        context.commit('setData', data)
      },

    }   })

Upvotes: 1

Views: 177

Answers (1)

Boussadjra Brahim
Boussadjra Brahim

Reputation: 1

In your main.js try to dispatch that action in mounted hook :

import Vuex from 'vuex';
import store from './vuex/store';
const app = new Vue({
  el: '#app',
  store,

  mounted() {
    this.$store.dispatch('getData')

  }

});

Upvotes: 1

Related Questions