Computed v-for Vuejs

Asked

Viewed 283 times

1

I have a v-for

<div  v-for="(m, ind) in arrayMeses" :key="ind" v-if="m" class="mes">
    <table border="1" width="100%">
        <tbody> 
            <tr class="mes_title">
                <td colspan="7" class="mes_title font-14"> 
                    {{ m }} - {{ testeComputed(ind) }}
                </td>
            </tr>
        </tboddy>
    </table>

I want to run a method on each interaction When the data is changed run as well

I tried to

 computed: {
     testeComputed: function(ind) {
         console.log('m',ind)
         return 'teste' + ind
     },
 },

You are returning an object with all page elements I would like to receive the index or month name

  • Do you have a loop in a div with a single row table of a single column? Better study a little more HTML and then go to Vue.js

  • @Guilhermecostamilam All right? It’s just a piece of code. It’s working and assembling the table as needed. My problem is when to run Computed. I can run, but I cannot pass the parameter. If I do a console.log in the parameter appears the object of all page components

1 answer

1


You are using computed properties wrong. Think about computed as a read-only property.

See the example below:

<div id="exemplo">
  Mensagem ao contrário: {{ mensagem.split('').reverse().join('') }}
</div>

To remove "raw" logic from the template, a computed Property is used.

Expressions within templates are very convenient, but are intended for simple operations. Putting a lot of logic into them can make them swollen and their maintenance more complicated. ("Computed and Observer Data", from documentation)

The result:

<div id="exemplo">
  <p>Mensagem ao contrário: {{ mensagemAoContrario }}</p>
</div>

var vm = new Vue({
  el: '#example',
  data: {
    message: 'Olá Vue'
  },
  computed: {
    mensagemAoContrario: function () {
      return mensagem.split('').reverse().join('');
    }
  }
})

If you really want to parameterize this, you can use a field in data or even a method, in methods.

  • I decided using a same method & #Xa;Thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.