Answer the question
In order to leave comments, you need to log in
Why do not have time to load the data in the state?
There is an asynchronous action that causes a mutation and updates the state data
actions: {
async fetchData ({commit}) {
await axios.get('apiurl')
.then(response => {
commit('setData', response.data)
})
}
}
computed: {
...mapState(['data'])
},
methods: {
...mapActions(['fetchData'])
},
created () {
if (!this.data) {
this.fetchData()
console.log(this.data) //undefined
}
}
Answer the question
In order to leave comments, you need to log in
actions: {
fetchData({commit}) {
return axios.get('apiurl').then(response => {
commit('setData', response.data)
})
}
}
computed: {
...mapState(['data'])
},
methods: {
...mapActions(['fetchData'])
},
async created () {
if (!this.data) {
await this.fetchData();
console.log(this.data)
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question