JavaScript Intialize Vue data with the result of an AJAX call initialize with data transfer,initialize data from props vue,initialize

I need to initialize a Vue component's data with the result of an AJAX call. I tried the following:

data: function () {
  return {
    supplierCount: 0
  }
},

created: function () {
  axios.get("/supplier/list").then(response => {    
    this.supplierCount = response.data.length;
  });
}

However, this approach doesn't work, because the template can access the data before the AJAX handler updates supplierCount.

What's the correct way to initialize the data with the result of an asynchronous call? For example, if I return a promise (instead of an object) from data, will Vue wait until the promise is rejected/resolved before exposing the data to the template?

Answer:1

I don't think you can force your component to initialize only after the ajax call, but you can configure it to be hidden before the ajax data is loaded, either by hiding it with css (using v-show) or by simply preventing its rendering (using v-if).

For example, you can add a property hasLoaded to your component, and bind either v-show or v-if to it, like this:

On your js:

data: function () {
  return {
    supplierCount: 0,
    hasLoaded: false
  }
},

created: function () {
  axios.get("/supplier/list").then(response => {    
    this.supplierCount = response.data.length;
    this.hasLoaded = true;
  });
}

On your template:

<!-- The top element is your root element, and you should always render it, so the v-show is appended to the immediate child -->

<div>
    <div v-show="hasLoaded">
        <!-- the rest of your template goes here -->
    </div>
</div>
Answer:2

I am stuck with ajax... I have a cart and it has gift vouchers, i) the code checks for valid voucher and if its not valid then it should show message "invalid voucher". ii) If voucher if valid and is ...

I am stuck with ajax... I have a cart and it has gift vouchers, i) the code checks for valid voucher and if its not valid then it should show message "invalid voucher". ii) If voucher if valid and is ...

  1. show message in view mvc
  2. show message in view laravel
  3. how to show message view in android studio
  4. show viewbag message in view
  5. show tempdata message in view
  6. show message from controller to view
  7. show error message in view mvc
  8. show error message in view from controller

JS models concurrency by an event loop. As a result there are no race conditions. So what are the drawbacks of the following type safe operation in the main scope of a program that would justify any ...

JS models concurrency by an event loop. As a result there are no race conditions. So what are the drawbacks of the following type safe operation in the main scope of a program that would justify any ...

  1. what is the typical class size at texas a&m
  2. what is the typical class size for ucla
  3. what is the typical class size at stanford university
  4. what is the typical class size at usc
  5. what are a typical class of anticancer drugs
  6. what is the typical class size at nyu
  7. what is the typical class size at texas tech
  8. what is the typical class size at asu
  9. what is the typical class size at unc
  10. what is the typical class size at gcu
  11. what is the typical class size at osu
  12. what is the typical class size at gsu
  13. what is the typical class size at pvamu
  14. what is the typical class size at wssu
  15. what is the typical class size at ttu
  16. what is the typical class size at csumb
  17. what is the typical class size at uc merced
  18. what is the typical class size at vsu
  19. what is the typical class size at apsu
  20. what is the typical class size at unr

I need to add class to input parent if input is not empty or focus because I want to change label position. <div class="input-field"> <input type="email" name="email" ng-model="email" ...

I need to add class to input parent if input is not empty or focus because I want to change label position. <div class="input-field"> <input type="email" name="email" ng-model="email" ...

I am trying to change custom icon of video, when video is toggled(Play/Pause). ngAfterViewInit() { const vdoCont = document.querySelector('.video-player'); const vdo = vdoCont.querySelector('video')...

I am trying to change custom icon of video, when video is toggled(Play/Pause). ngAfterViewInit() { const vdoCont = document.querySelector('.video-player'); const vdo = vdoCont.querySelector('video')...

  1. javascript snake
  2. javascript foreach
  3. javascript map
  4. javascript for loop
  5. javascript array
  6. javascript substring
  7. javascript switch
  8. javascript reduce
  9. javascript date
  10. javascript regex
  11. javascript filter
  12. javascript string contains
  13. javascript function
  14. javascript replace
  15. javascript slice
  16. javascript split
  17. javascript settimeout
  18. javascript download
  19. javascript interview questions
  20. javascript promise