Answer the question
In order to leave comments, you need to log in
How to track that data has been added to vuex?
Hello.
There is a Workspace.vue component, it has a fetch() method that is called in mounted()
The fetch() method sends a request to the server and puts the received data in vuex - this.$store.dispatch('setProject', response.data)
In the Workspace component The .vue is also connected to the NavBar.vue component. It has a div that needs to be displayed only when the creator of the project === the current user.
In NavBar.vue, I added the isShow() property to the computed to hang it later on the div v-if="isShow", but when the page loads, it writes an error that there is no id property, but then NavBar.vue is rendered. How to solve this situation and how in another component to wait until the fetch () method puts data from the store?
computed: {
...mapGetters([
'getProject',
]),
isShow() {
return this.getProject() && this.getProject().creator.id === this.user.id
},
},
Answer the question
In order to leave comments, you need to log in
writes an error that there is no id property
isShow() {
return ((this.getProject() || {}).creator || {}).id === this.user.id;
},
how in another component to wait until the fetch() method puts data from the store?
v-if="данные"
to the element/component that uses this data.
Perhaps not the best, but just a working method:
When creating state, assign null to your variable.
Then you import it
import { mapState } from "vuex"
...
computed: {
...mapState(['ТВОЯ_ПЕРЕМЕННАЯ'])
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question