Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use slot in a vuejs loop?

I have a template that loops using v-for. In the template I have a named slot with the name being dynamically assigned in the loop. No content is appearing, what am I doing wrong?

<todo-tabs :list="items">
    <div slot="interview">Interview</div>
    <div slot="membership">Membership</div>
    <div slot="profile">Profile</div>
    <div slot="handoff">Handoff</div>
</todo-tabs>

<template id="todo-tabs">
            <div class="tab-content ">      
                <div v-for="item in list" :class="{'active': item.current}" class="tab-pane" id="@{{ item.id }}">
                    <div class="skin skin-square">
                    <form class="form-horizontal" role="form">
                    <div class="form-body">
                        <slot name="@{{ item.id }}"></slot>
                    </div>
                    <div class="form-actions">
                        <button type="submit" class="btn red btn-outline">Submit</button>
                        <button type="button" class="btn default">Cancel</button>
                    </div>
                    </form>
                    </div>
                </div>
            </div>
        </template>

<script>
Vue.component('todo-tabs', {
        template: '#todo-tabs',
        props: ['list']
 });
 var vm = new Vue({
el: "#todo",
data: {
items : [
            {id: 'interview', name: 'interview', complete: true, body: 'something1', step_content: 'SOME'  },
            {id: 'membership', name: 'membership', complete: false, body: 'something2', step_content: 'SOME' },
            {id: 'profile', name: 'profile', complete: false, body: 'something3', step_content: 'SOME', current: true  },
            {id: 'handoff', name: 'handoff', complete: false, body: 'something4', step_content: 'SOME'}
        ]
    }
});
</script>
like image 356
jhodgson4 Avatar asked Dec 11 '15 15:12

jhodgson4


2 Answers

In VueJs version 2.1.3 you can use this:

parent:

<div v-for="row in rows">
    <slot name="buttons" :row="row"></slot>
</div>

child:

<template slot="buttons" scope="props">
    <a href="props.row.href">go there</a>
</template>

This construction does not generate any warning, therefor i think its valid.

https://vuejs.org/v2/guide/components.html#Scoped-Slots

like image 90
alexwenzel Avatar answered Sep 30 '22 01:09

alexwenzel


in VueJS 1.0.16 you could do this in your template:

<div v-for="item in tiems">
    <slot :name="item.id"></slot>
</div>

However, as of 1.0.17 VueJS throws this error: <slot :name="item.id">: slot names cannot be dynamic.

like image 38
ierdna Avatar answered Sep 30 '22 00:09

ierdna