W
W
WebDev2018-12-10 11:03:56
Vue.js
WebDev, 2018-12-10 11:03:56

How to correctly wait for data in a vue component?

When downloading the application, the user is authorized. I take the user token from localStorage and send it to the server for data.
Further, in one of the components (the form for adding a comment), the user needs to fill in three fields: name, email and message. The email field is populated from his profile by default. Filled in like this:

data() {
    name: '',
    email: '',
    message: ''
},
mounted() {
    this.email = this.$store.state.user.email;
}

The problem is that at the time mounted() is executed, user data may not yet arrive, that is, the request has not yet been fully completed. I could directly insert data from Vuex or hang up tracking through watch, but I doubt which is more correct.
What do you do in such cases?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
nvdfxx, 2018-12-10
@kirill-93

Just put all calls from the store in computed, not mounted

A
Anton Anton, 2018-12-10
@Fragster

Why duplicate the store data in the component data? why not use mapState ?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question