A
A
Andrej Sharapov2019-08-02 13:10:47
Vue.js
Andrej Sharapov, 2019-08-02 13:10:47

How to set mounted for list items?

Script:

In it, mounted counts time . Provided that time is not an element of the list.
Tell me how to rebuild the script so that the values ​​​​from the list are also considered from 0 (startVal) to endVal ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2019-08-02
@Madeas

Make a separate counter component and pass data to it:

<div id="app">
  <fucking-counter v-for="n in counters" v-bind="n"></fucking-counter>
</div>

Vue.component('fucking-counter', {
  props: [ 'start', 'end', 'title', 'delay' ],
  data: () => ({
    val: null,
  }),
  template: `<div>{{ title }}: {{ val }}</div>`,
  created() {
    this.val = this.start;
    const interval = setInterval(() => {
      if (++this.val >= this.end) {
        clearInterval(interval);
      }
    }, this.delay);
  },
});

new Vue({
  el: '#app',
  data: {
    counters: [
      { start: -20, end: 100, title: 'Counter 1', delay: 69 },
      { start:   0, end:  50, title: 'Counter 2', delay: 187 },
    ],
  },
});

https://jsfiddle.net/xsu57m9g/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question