mafortis
mafortis

Reputation: 7128

Vuex dispatch doesn't return data in view

I cannot get my data from vuex and use them in all views, but i do see their success results in network tab.

screenshots

network tab

one

console

two

Code

store.js

state: {
    currency: {}
},
mutations: {
    currency(state, currency){
      state.currency = currency
    }
},
actions: {
    currency({commit}){
      return new Promise((resolve, reject) => {
        commit('currency')
        axios({url: '/api/currencyDefault', method: 'GET' })
        .then(resp => {
          const currency = resp.data.data
          commit('currency', currency)
          resolve(resp)
        })
        .catch(err => {
          commit('currency')
          reject(err)
        })
      })
    },
},
getters: {
    currency: state => state.currency,
}

App.vue (main component where routers will load)

<script>
export default {
  props:['currency'],
  data() {
    return {
      isCollapse: true,
    }
  },
  created () {
    this.currency()
  },
  methods: {
    currency() {
      this.$store.dispatch('currency')
   }
  },
}
</script>

An then in my other component i call for currency like:

{{currency.name}}

ideas?

Upvotes: 0

Views: 45

Answers (1)

Qonvex620
Qonvex620

Reputation: 3972

Method Currency is already defined as props. remove this in your code.

props:['currency']

Ten call this currency in your component like this

<div>
   {{this.$store.getters.currency.name}}
</div>

to surely display the currency, what I did is to put first a condition to check if it is was already loaded. like this

<div v-if="$store.getters.currency">
      {{this.$store.getters.currency.name}}
  </div>

or declare a new variable in your data like

data() {
   return {
       currency: this.$store.getters.currency.name
   }
}

now you could call it this way

<div v-if="$store.getters.currency">
     {{currency.name}}
</div>

Upvotes: 1

Related Questions