Vuejs Errors before the API loads

Asked

Viewed 203 times

0

Is there any way I can ask for the Vuejs wait a while before dealing with the mistakes of the GIFT?

I have for example a select who receives a v-model coming from the API, but as Vuejs loads the DOM first before the call http, it already accuses error in select

type check failed for prop "options". Expected Array, got Undefined.

That mistake is right because he draws the GIFT with nothing on select, but then he fills in with my call to API. Can I ask Vuejs to wait a while for me to bring the data from API and then he checks the GIFT?

Thank you!!

Note: I know that starting the parameter with empty array works, but wanted another way without being manual

@Edit

.Vue

    <vue-select track-by="Value" v-model="data.nome" label="Name" :options="data.nomeList"></vue-select>

data(){
   data: {}
}

.Js

axios.get('url.com').then(response => {
     this.data= response.data;
})
  • 2

    Post the code...

  • 1

    There are several ways to control this flow/logic before there is data to show or render. Show the components where you have the problem to understand better. But you could have a v-if="select.options" in the parent component.

  • @Rafaelaugusto doesn’t have much to post... but anyway, I put an example there. @Sergio putting one v-if to check whether the v-model empty is a way out, but it’s still very manual... there’s another way? =/

  • In the data that nomeList should be nomeList: []. How do you get it there?

  • Exactly @Sergio, I don’t have nomeList defined, and neither want, because it is very manual remember all parameters and initialize one by one. That array comes from the API. You can do Vue waiting for the call http before presenting the error of expects an array in my input?

  • 1

    In Vue the recommended way is to set this in data. You could do :options="data.nomeList || []" but I advise against it because I find it useful to define what is reactive in data and thus have information of what properties are used by the component.

  • 1

    Got it Sergio!! Thank you :)

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.