VueJS - Component inside of v-for

19,765

You should pass then as dynamic prop using : in front of parameters:

<listcard :title=list.title :text=list.text></listcard>

From documentation:

A common mistake beginners tend to make is attempting to pass down a number using the literal syntax:

<!-- this passes down a plain string "1" -->
<comp some-prop="1"></comp>

However, since this is a literal prop, its value is passed down as a plain string "1", instead of an actual number. If we want to pass down an actual JavaScript number, we need to use the dynamic syntax to make its value be evaluated as a JavaScript expression:

<!-- this passes down an actual number -->
<comp :some-prop="1"></comp>

https://vuejs.org/guide/components.html#Literal-vs-Dynamic

Share:
19,765

Related videos on Youtube

Brotzka
Author by

Brotzka

I love coding, especailly with php and other webdevelopment-languages. Programming compared with conversion optimization and other e-commerce related topic is my specialty. That's why I'm studying e-commerce right now. I'm a professional web-developer at the Würth IT company in germany. Würth IT is part of the big b2b company Würth. In my free time I love trying out new technics.

Updated on June 04, 2022

Comments

  • Brotzka
    Brotzka almost 2 years

    I am trying to render a list of objects from my Vue-Instance. Each object should use a component, so I put the component into the v-for-loop. But all I get is list.title and list.text instead of the correct values.

    Is there a special way to use components in v-for-loops?

    I found this thread in the Vue-Forum, but don't know how to use it or if it's the right way.

    App:

    <div id="app">
        <div v-for="list in lists">
            <listcard title="list.title" text="list.text"></listcard>
        </div>
    </div>
    

    Template:

    <template id="listcard-template">
        <div class="card">
            <h2>{{ title }}</h2>
            <p>{{ text }}</p>
        </div>
    </template>
    

    My component:

    Vue.component('listcard', {
        template: '#listcard-template',
        props: ['title', 'text']
    })
    

    Vue-Instance:

    new Vue({
        el: "#app",
        data: {
            lists: [
                {title: "title1", text: "text1"},
                {title: "title2", text: "text2"},
                ...
            ]
        }
    })
    

    Thanks!