(New to Vue.js) I fetch data from a get request to display calendar information. I want this to update every 5 minutes.
Nothing in the docs about auto reload - how would I go about implementing this? Do I use standard javascript within the file or something else?
My complete app.js below:
Vue.component('events', { template: '#events-template', data: function() { return { list: [] } }, created: function() { this.fetchEventsList(); }, methods: { fetchEventsList: function() { this.$http.get('events', function(events) { this.list = events; }).bind(this); } } }); new Vue({ el: 'body', });
The best way to force Vue to re-render a component is to set a :key on the component. When you need the component to be re-rendered, you just change the value of the key and Vue will re-render the component.
Vue. component("Timer", { template: "#timer", props: { deadline: { type: String, required: true }, speed: { type: Number, default: 1000 } }, data() { return { currentTime: null }; }, mounted() { setTimeout(this. countdown, 1); }, methods: { countdown() { let t = Date.
You can force-reload components by adding :key="$route. fullPath". However, :key="$route. fullPath" only can force-reload the components of the different route but not the components of the same route.
To persist Vuex state on page refresh, we can use the vuex-persistedstate package. import { Store } from "vuex"; import createPersistedState from "vuex-persistedstate"; import * as Cookies from "js-cookie"; const store = new Store({ // ...
No need to re-invent the wheel, window.setInterval()
does the job pretty well
Vue.component('events', { template: '#events-template', data () { return { list: [], timer: '' } }, created () { this.fetchEventsList(); this.timer = setInterval(this.fetchEventsList, 300000); }, methods: { fetchEventsList () { this.$http.get('events', (events) => { this.list = events; }).bind(this); }, cancelAutoUpdate () { clearInterval(this.timer); } }, beforeDestroy () { this.cancelAutoUpdate(); } }); new Vue({ el: 'body', });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With