3

On this page, it says that:

In situations where computed properties are not feasible (e.g. inside nested v-for loops), you can use a method

However, I am able to use computed props inside a nested v-for loop (check fiddle)

new Vue({
  el: '#sample',
  data() {
    return {
      numbers: [1, 2, 3, 4, 5]
    }
  },
  computed: {
    even() {
      return this.numbers.filter(n => n % 2 === 0)
    }
  }
})
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<div id="sample">
  <div v-for="n in numbers">
    {{n}}
    <div v-for="e in even">
      ..{{e}}
    </div>
  </div>
</div>

What am I missing?

1
  • In the doc both examples return the same. The “method” option is mentioned to indicate that it is also possible to iterate on the result of a method with v-for. Like the doc says, it is merely useful when working with nested lists (i.e. list dynamically filtered from its parent’s list element). This avoids creating nested computed properties. Commented Feb 14, 2018 at 11:38

2 Answers 2

2

Okay so I believe the docs mean that computed properties are not feasible inside nested v-for loops, if, for instance, the inner array depends on the current outer array element.

As a quick example, imagine we want a loop which displays numbers from 1 through 5. And after each number N, we need a nested loop which will contain all the whole numbers leading up to the number N.

Check fiddle to see what I'm talking about.

new Vue({
  el: '#sample',
  data() {
    return {
      numbers: [1, 2, 3, 4, 5]
    }
  },
  methods: {
    getLeadingNbs(n) {
    	return (Array(n)+'').split(',').map( (n,i) => i )
    }
  }
})
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<div id="sample">
  <div v-for="n in numbers">
    {{n}}
    <div v-for="l in getLeadingNbs(n)">
      ....{{l}}
    </div>
    <br>
  </div>
</div>

Sign up to request clarification or add additional context in comments.

Comments

0

The docs mean that it is not possible to have a computed for each element of numbers because computeds cannot be created dynamically. It is, however, possible to make a computed that returns an array with one value for each element of numbers.

4 Comments

Don't you mean, ...make a method ..., instead of computed ?
No, you can actually make a computed based on an array, using map.
Gotcha. So I guess a method is necessary whenever the inner loop is computed against a particular value from the outer loop. Of the top of my head, an example could be: say the outer loop renders 1, 2, 3, and each of the 5 inner loops should display all the natural numbers leading up to that number. So we have 1 (1), 2 (1,2), 3 (1,2,3) This can't be done simply with computed values, right?
Right. It becomes impractical.

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.