Steps for Displaying Data from API Response in Vue.js

Immediately rendering the following template, without waiting for the API call to complete.

I came up with a solution using v-if to prevent elements from rendering until the data is available.

However, this seems to go against the DRY principle as I have to wrap my elements with v-if statements.
Is there a different approach to tackle this issue? Another way to code it?

<template>
    <div id="app">
        <div v-if="obj">
            <h2>{{ obj[0].item }}</h2>
        </div>
        <div v-if="obj">
            <h5>{{ obj[0].id }}</h5>
        </div>
    </div>
</template>
<script>
export default {
    name: 'app',
    data() {
        return {
            obj: []
        }
    },
    mounted: function() {
        axios.get(URL)
            .then(response =>
                this.obj = response
            });
}
}
</script>

Answer №1

A solution is available if you are utilizing Vue Router - https://router.vuejs.org/en/advanced/data-fetching.html

The data will be fetched immediately after the route is activated by monitoring the $route object

You can also refer to a similar answer I provided here: Check permissions before vue.js route loads

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

Selecting options on hover, either A or B at the least

I need a jQuery selector to handle an 'either' scenario. Specifically, I have a dropdown menu and want it to stay open when the user hovers over either of two elements. Either when they hover over the button or when they leave the popped-out men ...

The issue with Array.prototype.join in Internet Explorer 8

In my current working scenario, I encountered an issue with the following code snippet. It performs well in the latest versions of Internet Explorer (IE), but the join function fails to work correctly in IE 8 Version. <!DOCTYPE html> <html xmlns= ...

What causes the variance in behavior between the Angular-formly directive and type?

I am facing an issue with two input fields that are both generated using the same template. I have set the required attribute to true for both of them by using the following code snippet: ... templateOptions: { ... required: true } One input fiel ...

Loading Data in CodeMirror using XMLHttpRequest (XHR)

Is there any progress in loading the content into CodeMirror using XHR? ...

My code remained untouched, but suddenly I encountered an error stating: "TypeError: Cannot destructure property 'type' of 'vnode' as it is null." This error appeared when I tried launching my web application

My web application is connected to an API, and normally everything runs smoothly. However, suddenly without making any changes to my code or the API itself, it stopped working and I started receiving a multitude of errors like the one shared on my web appl ...

The AngularJS element fails to display on the screen

I am currently learning AngularJS and I am struggling to understand how components are linked to the view in the tutorial. I have created a component that is quite similar: angular.module('phonecatApp').component('nameList', { temp ...

Animate an element when switching routes

Is there a way to smoothly transition an SVG element across a page when the route changes in Vue.js? I've attempted to set up a watcher that triggers an animation based on the route path conditions. Although the transitionName updates correctly, the ...

Obtain an item from an array by utilizing the property with Lodash _.find

I currently have an array of objects called memberToChange.checkboxes: ICheckbox[] structured like this: https://i.stack.imgur.com/LyKVv.png Within this setup, there is also a variable named internalNumber with the value "3419". My objective is to locate ...

Looking for a simple method to display a popover using conditional if/else statements?

On my website, I am utilizing JavaScript to determine the user's Facebook status. This includes checking if the user is already logged in and has allowed our app, if they are logged in but haven't authorized our application, or if they are not lo ...

Exploring the realm of unit testing in the NestJS CQRS architecture journey

We're currently working on writing unit tests using Jest for our application and are facing difficulties in testing sagas. Specifically, we're having trouble testing the saga itself. During our unit testing process, we've encountered an iss ...

Glistening tabPanel and Analytics by Google

I recently completed a comprehensive tutorial here that delves into the intricacies of Google Analytics. Despite grasping the concepts explained in the tutorial, my understanding of jQuery remains at ground zero. In my ShinyApp, I utilize multiple tabPanel ...

the function does not output any value

I am currently facing an issue with my function called IsValidUrl(). This function is supposed to return values based on a certain condition (either false or true). However, there seems to be another function within it that is preventing the values from be ...

access the content of a div element by its class attribute

Within the div element, a value has been set and I am attempting to retrieve this value using its class. However, the code below does not return the desired value. What mistake am I making here? How can I resolve this issue? <div title="Find on Amazo ...

The process of uploading a file is interrupted by an AJAX Timeout

My HTML form includes a file input field that utilizes AJAX to upload the selected file, complete with a progress bar. However, I encountered an issue where the request would hang without any response. To prevent this from happening in the future, I aim t ...

What is the order of reflection in dynamic classes - are they added to the beginning or

Just a general question here: If dynamic classes are added to an element (e.g. through a jQuery-ui add-on), and the element already has classes, does the dynamically added class get appended or prepended to the list of classes? The reason for my question ...

Unveiling the Secrets of Encoding and Decoding JSON within a Concealed HTML

I am in the process of creating a unique control using HTML and JQuery that will showcase a specific text value. Users will have the ability to input various key/value pairs. Here is the current code snippet I am working with: <input id="keyValue" type ...

Communication between AngularJS directives and controllers occur when a function is called upon a change

I have developed a unique custom directive which is defined as: <div class="col-md-6"> {{templateMapping[colProp].SheetPointer}} <select class="form-control" ng-model="selectedColumn" ng-change="changeMapping()" ng ...

Having trouble with shipit.js deployment: Error encountered - git checkout undefined

I have been using shipit.js to deploy my nodejs application on an Ubuntu 16.04 server successfully. However, I recently encountered the following error: ./node_modules/shipit-cli/bin/shipit production deploy start Running 'deploy:init' task... ...

What is the best way to inject child nodes into parent nodes without causing the view to shift to the location where the element is being

While I am rendering elements and appending them to a parent div, my screen keeps jumping to the bottom-most element as it loads instead of maintaining the current view. Ideally, it should start at the top of the screen and remain there without any sudden ...

Explore vuetify data-table functionality by performing searches without any special characters

I am working with the Vuetify data-table component and I would like to implement a search functionality that ignores special characters. For instance, if I have the Polish name "Łukasz" in my table, I want it to be found when I type "lukasz". Is there a ...