0

I work with constantly changing api data. I use Laravel and Vue.js. There is a steady stream of data when I control the network with F11. But it has no effect on the DOM.

Here are sample codes. I would be glad if you help.

HTML code;

    <div class="row">
         <div class="col-md-12">
               <p class="tv-value" v-html="totalMeetings"></p>
          </div>
    </div>

Script Code;

    <script>
        export default {
            data() {
                return {
                    totalMeetings: null
                }
            },

            created() {
                this.getPosts();
            },

            methods: {
                getPosts() {
                    axios
                        .get("/get-total-meetings")
                        .then(response => (this.totalMeetings = response.data))
                        .catch(error => {
                            this.errors.push(error);
                        });
                }
            },

            mounted() {
                setInterval(function () {
                    axios
                        .get("/get-total-meetings")
                        .then(response => (this.totalMeetings = response.data))
                        .catch(error => {
                            this.errors.push(error);
                        });
                }, 2000)
            }
        }
    </script>

1 Answer 1

1

Change your setInterval function to arrow function like this.

setInterval(() => {
       axios
            .get("/get-total-meetings")
            .then(response => (this.totalMeetings = response.data))
            .catch(error => {
            this.errors.push(error);
        });
}, 2000);

You could put a watcher for that to be able vue to watch the changes of your data. like this.

watch: {
  totalMeetings(val) {
       this.totalMeetings = val
  }
}

Or create a computed property for it to update the value when it changes.

computed: {
    total_meetings() {
       return this.totalMeetings
    }
}

then your component should look like this

<p class="tv-value" v-html="total_meetings"></p>
Sign up to request clarification or add additional context in comments.

5 Comments

Sorry, It looks like the method I did and it didn't work.
what do you mean method you did ?
Review the code I sent. You will see there. @Qonvex206
Its because when you are not using arrow function in vue, then the this keyword will not work inside the function.
Its because when you are not using arrow function in vue, then the this keyword will not work inside the function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.