Answer the question
In order to leave comments, you need to log in
How to get the correct result when rendering a component?
There is a component. He has:
async created() {
buildRequestCurTour(this.$route.params.id)
await this.getCurrentTour()
},
mounted() {
console.log(this.currentTourGetter)
},
methods: {
...mapActions(['getCurrentTour'])
},
computed: {
...mapGetters(['currentTourGetter'])
}
async getCurrentTour(ctx) {
const res = await fetch(apiUrl, {
method: 'POST',
mode: 'cors',
cache: 'no-cache',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json'
},
redirect: 'follow',
referrerPolicy: 'no-referrer',
body: JSON.stringify(this.getters.requestCurTourGetter)
})
const tour = await res.json()
ctx.commit('updateCurrentTour', tour.hits.hits[0])
}
updateCurrentTour(state, currentTour) {
state.currentTour = currentTour
},
currentTourGetter: state => state.currentTour,
Answer the question
In order to leave comments, you need to log in
Well, everything is fine, the component does not wait for anyone and is mounted to itself synchronously. When the request fulfills then the value will be. Because it's a getter, vue will update everything when needed. Just show some spinner thread while currentTourGetter is empty.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question