V
V
valexeich2022-03-23 15:15:38
Vue.js
valexeich, 2022-03-23 15:15:38

How to make it so that when adding a new object to json, this object dynamically appears on the page without reloading the page?

I get a json object using axios, this is a list of objects in my database and I display it on the page in a cycle, I want it to dynamically appear on the page without reloading the page when an object is added to the database, now it only appears when the page is reloaded

Here is the code:

new Vue({
    el: '#items_app',
    data: {
    items: []
    },
    created: function () {
        const vm = this;
        axios.get('/api/list/')
        .then(function (response) {
        vm.items = response.data
        })
    }
}

)


html:

<div id="items_app">

        <div v-for="item in items">
            <p style="color: white">{{ item.name }}</p>
        </div>

    </div>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Anton, 2022-03-23
@valexeich

Either by a button to re-request data, or by a timer (not a very option), or through a web socket / services like pusher from the server, send an event to the client, by which to request new data (well, or send this data directly in the body of the event)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question