B
B
bqio2019-08-04 10:07:14
Vue.js
bqio, 2019-08-04 10:07:14

How to call a method on each element during list rendering?

<div class="profiles">
  <canvas width="50" height="50" v-for="p in profiles" :data-avatar="p.avatar"></canvas>
</div>

let app = new Vue({
    el: '#app',
    data: {
        profiles: []
    },
    methods: {
        renderAvatars () {}
    },
    mounted: function () {
      this.profiles.push({
                name: 'test',
                avatar: 0
      });
    }
});

How do I call the renderAvatars method on every iteration when rendering each canvas element?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
0
0xD34F, 2019-08-04
@bqio

Put on ref elements, in mounted (first render) / updated (subsequent renders) call the desired method. You just need to remember that the positions of the object in the array and the reference to the corresponding element are not guaranteed to match, so you have to set the correspondence manually (for example, using the data attribute). How it might look . And it is better of course to make a separate component .

V
Viktor K, 2019-08-04
@vuldozer

With standard vue tools, this, it seems, cannot be done. You can use render functions to control the output.
Read here https://ru.vuejs.org/v2/guide/render-function.html
You can hack the method call when changing the DOM ( DOMNodeInserted DOMNodeRemoved ), but this is terrible shit. Can I get more details on the issue? What do you want to achieve in the end? What are the processes there?

D
Daniil Bratukhin, 2019-08-04
@dantothefuture

It seems to me that it makes sense to put the canvas in a separate component and start rendering in mounted (). So all the logic will be in one place, and you won’t have to worry about the $refs array.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question