最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - VueJS - Component inside of v-for - Stack Overflow

programmeradmin5浏览0评论

I am trying to render a list of objects from my Vue-Instance. Each object should use a ponent, so I put the ponent 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 ponents 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 ponent:

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

Vue-Instance:

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

Thanks!

I am trying to render a list of objects from my Vue-Instance. Each object should use a ponent, so I put the ponent 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 ponents 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 ponent:

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

Vue-Instance:

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

Thanks!

Share Improve this question asked Aug 15, 2016 at 7:05 BrotzkaBrotzka 2,7854 gold badges39 silver badges62 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 14

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

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

From documentation:

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

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

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 -->
<p :some-prop="1"></p>

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

发布评论

评论列表(0)

  1. 暂无评论