0

So the following template is rendered immediately, and it does not wait for the API call.

The solution I found is using v-if in order to keep the elements from rendering until the data is there.

This seems counterintuitive to the DRY principle if I have to wrap my elements with v-if.
Is there another approach to this problem? Another way of coding this?

<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>

1 Answer 1

1

Yes there is if you are using Vue Router - https://router.vuejs.org/en/advanced/data-fetching.html

So data would be fetched immiteately after the route is activated by watching the $route object

Check my similar answer here Check permissions before vue.js route loads

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.