question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`v-for` directive with integer range does not work with constant attribute but only works with binding

See original GitHub issue

I was confused by some strange behaviour related to the v-for directive (Vue 2.5) with integer ranges and I ended up posting this stackoverflow question about it. The question was promptly answered and I now understand my mistake but I think that this behaviour is either a bug or a failure in the documentation and so I am opening this issue anyway.

Brief Summary

Here is a component:

let CounterComponent = Vue.extend({
    template: `
        <div>
            <h3>The counter is {{value}}</h3>
            <svg v-for="n in value" :key="n"
                 xmlns="http://www.w3.org/2000/svg" version="1.1"
                 xmlns:xlink="http://www.w3.org/1999/xlink"
                 viewBox="0 0 10 10">
                <circle cx="5" cy="5" r="5"></circle>
            </svg>
        </div>
    `,
    props: {
        value: Number
    }
})

Note the use of the v-for directive with an integer range bound to the value property.

If you use a v-for as in this jsfiddle, it doesn’t work at all:

<counter-component value="14" />

However, the following both work: jsfiddle, jsfiddle

<counter-component v-bind:value="15" />
<counter-component :value="16" />

Source of Great Confusion™

I was fooled by the fact that the interpolation in the h3 tag works in all three cases but the v-for directive only works in the latter two. I wasted hours trying various variants of the syntax because, while I will certainly use a binding in a live application, I was trying out the integer-range support in the v-for directive for the first time and had plonked multiple instances of the component I was building onto a page with different values of the value property, all set as constants.

Suggested Resolution

I propose that either v-for is changed so that it works in all three cases or that the documentation (https://vuejs.org/v2/guide/list.html#v-for-with-a-Range) is updated to reflect this requirement that, if a component property is used as the range parameter, that property cannot be set to a literal and must be bound.

I am not sure how to phrase the latter but I guess that simply updating the docs would be the responsible course of action.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
yyx990803commented, Mar 16, 2018

A literal prop is passed down as a string, not a number.

0reactions
yyx990803commented, Mar 16, 2018

You did not get a warning because it is technically possible to iterate over a string with v-for as well.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Vue `v-for` directive with integer range bound to component ...
Bind v-for to an array and hook into changes of the counter property to update that internal array using only array mutations that...
Read more >
Top 18 Most Common AngularJS Developer Mistakes - Toptal
Sheer size of the AngularJS can easily lead to many mistakes. In this article you will learn about most common AngularJS developer mistakes...
Read more >
Angular ngFor - Learn All Features, Not Only For Arrays
In this post we are going to go over the ngFor core directive, namely we are going to go over the following: what...
Read more >
Structured binding declaration (since C++17)
Binds the specified names to subobjects or elements of the initializer. Like a reference, a structured binding is an alias to an existing...
Read more >
ASP.NET Core Blazor routing and navigation - Microsoft Learn
Learn how to manage request routing in Blazor apps and how to use the Navigation Manager and NavLink component for navigation.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found