0

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?

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

1 Comment

nice one, it's working, but is there a way to make this.$store.getters.currency shorter? like we just call {{currency.name}}

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.