Skip to content

<slot :name="xxx"></slot> :slot="xxx" not worked in v-for loop #2511

@ghost

Description

In my code, a component is registered with different slot names calculated in a v-for loop as below:

//template
<script type="text/x-vue-template" id="slot-parent-template">
    <ul>
        <li v-for="item in items">
            <h1>{{item}}</h1>
            //slot name is computed by item (data type Number)
            <slot :name="item"></slot>
        </li>
    </ul>
</script>
//register component and init instance in js
<script type="text/javascript">
    Vue.component('slot-parent',{
        props:['items'],
        template:"#slot-parent-template"
    });
    new Vue({
        el:'body',
        data:{
            items:[1,2,3,4,5]
        }
    });
</script>

and below code works fine :

//in html
<slot-parent :items="items">
    //insert every slot manually works fine
    <p slot="1">1</p>
    <p slot="2">2</p>
    <p slot="3">3</p> 
    <p slot="4">4</p>
    <p slot="5">5</p>
</slot-parent>

but when I changed the code as below, slot can't be inserted into the component view,

//in html
<slot-parent :items="items">
   // insert slots with v-for loop, slot names are calculated by item
    <p v-for="item in items" :slot="item">{{item}}</p>
</slot-parent>

is the way I design this component wired ? or still there is any other way to solve the problem ?
I understand partial and component can be used in the slot-parent to do the same thing, but how about just do this with slot, since slot content can be written obviously in html file.

By the way, same problem found in Vue forum
http://forum.vuejs.org/topic/3448/dynamic-slot-names-not-working-anymore-in-new-version/5

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions