Vue: Is it possible to have properties like $first, $last, etc. as in AngularJS in v-for loops?

Created on 7 Sep 2016  ·  2Comments  ·  Source: vuejs/vue

Hello,

I found out in the VueJS documentation about lists, only $index exists as a special property in template lists. AngularJS has 6: $index, $first, $middle, $last, $even and $odd which I found really convenient.

For example, instead of writing this code:

<ul>
  <li v-for="item in items" :class="{'last': $index === items.length - 1}"
</ul>

We would write:

<ul>
  <li v-for="item in items" :class="{'last': $last}"
</ul>

Avoiding the well-known-and-always-the-same logic to know if we are talking about the last element or not, or the first element, or an odd element, etc...

Is it possible to get this part of VueJS 1.x.x?

Thanks!
Jerome

Most helpful comment

$index is deprecated in 2.0 in favor of a more explicit syntax: (item, index), because it has been concluded long ago that magically injected variables like $index will be a mess if you are in nested v-for(ng-repeat).

So please use the current solution you're using, it's only a few characters longer but everything is more explicit and won't give you any surprises, and you have full control of what's going on.

All 2 comments

(No comment on Vue 1)
In Vue 2 I don't see this being very feasible due to the v-for="(item, index) in items" syntax.
Maybe you could just write helper methods instead and add them in each component that needs them?

$index is deprecated in 2.0 in favor of a more explicit syntax: (item, index), because it has been concluded long ago that magically injected variables like $index will be a mess if you are in nested v-for(ng-repeat).

So please use the current solution you're using, it's only a few characters longer but everything is more explicit and won't give you any surprises, and you have full control of what's going on.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

robertleeplummerjr picture robertleeplummerjr  ·  3Comments

julianxhokaxhiu picture julianxhokaxhiu  ·  3Comments

aviggngyv picture aviggngyv  ·  3Comments

loki0609 picture loki0609  ·  3Comments

finico picture finico  ·  3Comments