Contrasting loading data from an empty state and having no items present

On my web page, I pull items from an API and display them. The API request is made in the created hook. I need to display a [loading] message while waiting for the API response, and a [no items] message if the loading is complete but there are no items available.

Right now, I have a computed property called IsEmpty:

isEmpty() {
  return this.cards.length == 0;
}

However, this property is triggered in both scenarios. How can I create a new property to handle both situations effectively?

Answer №1

To improve your code, consider adding a 'loading' data object property that is initially set to 'true' and then reset to 'false' in the API callback:

data(){
  return {
     loading:true
    }
 },
computed:{
   isEmpty() {
       return this.cards.length == 0 || this.loading;
  }
},
created (){
   axios.get(...).then(res=>{
      ...
     this.loading=false;
    })

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

Enhancing Security in the apex.server.process Function in Oracle APEX

Once we have disabled a button using client-side JavaScript, our goal is to initiate an Ajax call to create a record in a database table through an On-Demand Process. However, there is a concern that users could bypass this process by making similar calls ...

Utilize Vue.js to easily upload images alongside form input fields

I have recently started a small project with Vue Js. I am trying to incorporate an upload file option in my contact form. Due to the numerous input text fields, I am using serialize for the form. However, I am facing issues with the append function. How ca ...

Guide to effectively utilizing Vue slot-scope feature within nested components

I am currently facing difficulties in understanding the proper usage of the slot-scope attribute and comprehending the documentation. Below is a simplified version of my requirement. You can view it in action here: https://jsfiddle.net/4j4zsy0g/ Main Cod ...

The positioning of CSS arrows using the "top" attribute is not relative to the top of the page when using absolute values

I am currently working on positioning the arrow in the screenshot using TypeScript calculations. However, I am facing an issue where the position is being determined based on the top of the black popup instead of the top of the screen. From the top of the ...

Creating a network of communication between API routes in NextJS

Can the fetch API be used within an API route in NextJs? I have a large handler and instead of having everything in one handler, I'd like to modularize it so that after completing a specific task (e.g., writing to a database), I can call another handl ...

Signaling an Event from a module in the node_modules directory to the Vue application

Is there a way to capture an event emitted by a node module and receive it in a Vue file? Sample code from the JavaScript node module: const EventEmitter = require('events') class Find extends EventEmitter { // code snippets here } class Fin ...

Display only distinct items in a v-for loop in Vue.js

I am attempting to show icons based on specific v-for and v-if conditions. However, the icons are appearing multiple times when I only want them to display once. I attempted using v-if = 'index === 0', but this did not resolve the issue. <di ...

Retrieving a variable value set within a jQuery function from within an Angular 2 component

In the current project, I am facing a situation where I need to work around and initialize jQuery datetimepicker inside an Angular 2 application (with plans to refactor it later). However, when I assign a datetime value to a variable, I encounter a proble ...

The Trouble with Vue.js 2 and Axios Scopes

I previously encountered this "problem" but can't recall the correct approach to achieve the desired results. My current setup involves using Vue 2 to load data into variables that are then displayed on the HTML side: window.Vue = require('vue&a ...

Tips for calculating the total of an array's values

I am seeking a straightforward explanation on how to achieve the following task. I have an array of objects: const data = [ { "_id": "63613c9d1298c1c70e4be684", "NameFood": "Coca", "c ...

What is the best way to organize JSON files data in a specific sequence?

I successfully converted 3 JSON files into an HTML page using AngularJS. Here is the code I used: Factory code app.factory('myapp', ['$http', function($http) { function getLists() { var tab = ['url1', 'url2 ...

Using axios to render data in a view template

I am currently working on developing a basic Single Page Application (SPA) with the help of vue.js and axioz for scripts, without using CLI or any other tools. At this point, I have succeeded in fetching data from a JSON file, rendering and paginating the ...

Utilize moment.js to convert an epoch date into a designated time zone

I've spent countless hours searching for a resolution to the issue with moment.js and its inability to accurately display the correct date for a given local time zone. Let me explain my predicament: The flight API I'm utilizing provides me w ...

After deploying a Next.js project on an Ubuntu server, dynamic pages are returning a 404 error

I've been putting in a substantial amount of time on this project. I'm encountering some 404 errors on my dynamic pages, even though they work perfectly fine on localhost. I've tried using revalidate and playing around with fallback: true an ...

JavaScript element styling in a Partial view, experiencing issues with functionality

In the main view, the javascript element is working fine. However, in the partial view it seems to not be functioning even though it is correctly formatted. Any ideas on how to fix this issue? Check out this fiddle for reference: http://jsfiddle.net/bgrin ...

Can a Vue app be created as a standalone application?

Can a Vue application be created without requiring Vue as a runtime dependency? For example, rather than having the browser load vue.js and the app like this <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <script src= ...

Transferring session id across various devices using PHP

Here's the scenario: I have two users accessing different pages on separate devices - one user is on page 1 and the other is on page 2. My goal is to implement PHP forms on page 1 that can dynamically change the content displayed on the second user& ...

When using iOS, the video compressing process stops automatically if the screen is no longer active while the file input

I am working on a web application that includes a file upload feature for large videos, typically 30 minutes or longer in duration. When a user on an iOS device selects a video to upload, the operating system will automatically compress it before triggerin ...

Is it Vue's responsibility to automatically remove all Vue/Vuex watchers when a component is destroyed?

When a component subscribes to Vuex events using this.$store.watch or this.$store.subscribe, is it essential to manually remove the watcher during component destruction, or does Vue handle this internally? P.S: The current setup is based on version 2.6.10 ...

Unable to retrieve innerHTML from a jQuery element

My current task involves implementing the Google Maps API. The code snippet below is what I have written so far: <div id="map"></div> To inspect the content of $("div#map"), I am utilizing the Google Chrome console. console.log($("div#map")) ...