Is a preloader needed in a Vue.js app?

I'm currently exploring the world of Vue.js and could use some advice.

When making a GET request using the axios package, I would like to display a preloader for the entire page until all the data has been loaded. While I know this is a common task in Vue.js, I'm interested in learning how it's typically approached.

To create the preloader, I've developed a new component but I'm feeling a bit confused. How can I call that component from another one and make it visible at a specific time?

Answer №1

To display a preload user interface before making multiple requests using axios and the all & spread methods, you can follow this approach. First, show the preload UI with an animation, then execute your requests concurrently by using axios.all. Once all requests are completed, utilize the then method to hide the preload UI. Here is an example:

// Display preload UI
showSpinnerAnimation();

// Execute requests in parallel
axios.all([
  axios.get('https://examplelink'),
  axios.get('https://anotherlink')
])
.then(axios.spread(function (response1, response2) {
  // Handle responses after both requests are finished
  console.log('Response from first link', response1.data);
  console.log('Response from second link', response2.data);

  // Hide preload UI
  hideSpinnerAnimation();
}));

Answer №2

Axios operates in a different manner compared to ajax. It can be described as an advanced version of the javascript ajax function.

Here's a brief example of using axios:

 const axios = require('axios');

// Making a request for a user with a specific ID
axios.get('/user?ID=12345')
  .then(function (response) {
    // handling success
    console.log(response);
  })
  .catch(function (error) {
    // handling errors
    console.log(error);
  })
  .then(function () {
    // always executed
  });

// Alternatively, the same request can be made like this
axios.get('/user', {
    params: {
      ID: 12345
    }
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  })
  .then(function () {
    // always executed
  });  

// Interested in using async/await? Just add the `async` keyword to your outer function/method.
async function getUser() {
  try {
    const response = await axios.get('/user?ID=12345');
    console.log(response);
  } catch (error) {
    console.error(error);
  }
}

For more information, visit: https://github.com/axios/axios

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Vue not displaying information from API calls

After developing my own backend API, I encountered a strange issue. When I test the API on Chrome, it functions perfectly fine. However, when I attempt to consume the API using Axios in Vue, no data is returned. axios.get('http://192.168.149.12:8888/ ...

Back up and populate your Node.js data

Below is the Course Schema I am working with: const studentSchema = new mongoose.Schema({ name: { type: String, required: true }, current_education: { type: String, required: true }, course_name: { ...

The async waterfall is encountering a Typeerror due to the nextcallback function not being defined properly

async.waterfall([1,2,3,4].map(function (arrayItem) { return function (lastItemResult, nextCallback) { // performing the same operation for each item in the array var itemResult = (arrayItem+lastItemResult); // pa ...

When incorporating vue-router with vuejs3, an error message may appear: Vue warn - Component does not have a template or render function

I'm attempting to create a basic router using vue-router on vuejs3, and I encountered this warning upon clicking the link for the first time (but not subsequent clicks): vue@next:1571 [Vue warn]: Component is missing template or render function. The ...

Generate a JSON array containing objects consisting of a combination of string and boolean values

My goal is to generate a list containing objects with names and boolean values by utilizing AJAX. This process is initiated in the following manner: $('.si-accordion').click(function () { $(this).siblings('.accordion_tab&apo ...

The problem with Ajax POST is occurring in Firefox and Chrome, but not in IE8

I am encountering an issue with the code snippet provided below. Upon executing it in Internet Explorer 8, I receive an alert when the call is successful. However, this behavior does not occur in Firefox and Chrome. In other words, there is no alert disp ...

Identifying a flaw in an HTML5 video

I am attempting to identify when an error occurs while playing an HTML5 video. Specifically, I am encountering a situation where I am trying to play an HLS video on a MAC (where "canPlayType" is at least "maybe"), but the video will not play for unknown r ...

Display previous messages in React JS chat when scrolling upwards

I am currently working on a chat application, as depicted in the image. Once the chat is initiated, it automatically scrolls down to display the most recent messages. My goal is to implement a feature where when a user scrolls up to reach the oldest mess ...

The PageMethods function is successful when using a single parameter, but encounters an error when attempting to use two parameters, resulting in the

PageMethods worked perfectly when sending a single parameter to a code-behind aspx page. However, I encountered the error "Unknown web method" when attempting to provide two parameters (or an Object other than a String). The functional code in my aspx pag ...

Clearing Arrays in React Native Using useState

I'm struggling with the following code which aims to create an animated polyline for a map. I came across some examples online, but they were using outdated methods and didn't include useEffect or useState. I can't seem to clear the polylin ...

Scrolling to the top of the Popper component when an element is selected

I am currently utilizing the Autocomplete Material UI control with multiple selections enabled. Here is the code snippet I have created based on this answer: <Autocomplete PopperComponent={PopperMy} ... /> const PopperMy = function (props) ...

Debugging Success Message Display Issue in JavaScript

$.ajax({ type: "POST", url:'/saveLayout', data: {id : layoutId, section : arrSection}, success: function(response){ $('#successMsg').addClass("errorBox"); document.getEleme ...

Is there a way to make Bootstrap 5 load its jQuery plugins right away, rather than waiting for DOMContentLoaded?

After migrating to Bootstrap 5, I noticed that the following code is broken: <script src="https://code.jquery.com/jquery-3.6.0.js"></script> <script src="https://cdn.jsdelivr.net/npm/@popperjs/<a href="/cdn-cgi/l/email-prot ...

What is the best way to retrieve scope variables from multiple directives?

I am working on a directive that represents a person with changing location attributes. My goal is to access all the locations together and plot them on a map using the angular-leaflet-directive. I'm struggling to figure out how to access these variab ...

Collaborative Desktop & Web Application powered by Javascript REST/Ajax

Currently, my node.js back-end is seamlessly working with a web-based JavaScript client by sending AJAX requests. However, I am now contemplating creating a compact desktop version of the JavaScript client using solely JavaScript, specifically leveraging ...

Tips for incorporating a CSS transition when closing a details tag:

After reviewing these two questions: How To Add CSS3 Transition With HTML5 details/summary tag reveal? How to make <'details'> drop down on mouse hover Here's a new question for you! Issue I am trying to create an animation when t ...

Employ Angular within the parameters of a JavaScript function call

My goal is to incorporate Google Maps into my webpage for various locations (lieux). Within the HTML page, I have the necessary function set up for Google Maps. <script> function initialize(long,lat) { var mapCanvas = document.getElementById( ...

Unable to extract data from object list in Vue.js

Hi, I'm new to coding and trying to implement a function that shows related articles using Java with REST api. However, when I call the API using axios, the data is not being displayed on the view. I have checked the API and confirmed that it is retu ...

Incorporate titles for items in the jquery caroufredsel plugin

I am currently using the second example of the caroufredsel plugin, which can be found on this page . I am attempting to add a caption for each picture. To achieve this, I have used `li` and `lis` in my HTML code: <div class="list_carousel"> &l ...

Having trouble establishing a connection with localhost at port 4445 using Nightwatch and Selenium

I am encountering an issue while trying to execute my Nightwatch script in Javascript. The error message I am receiving is: \ Connecting to localhost on port 4445... ‼ Error connecting to localhost on port 4445. × failed E ...