我可以在vuejs循环中使用插槽吗?

jho*_*on4 4 javascript vue.js

我有一个使用v-for循环的模板.在模板中,我有一个命名槽,名称在循环中动态分配.没有内容出现,我做错了什么?

<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>
Run Code Online (Sandbox Code Playgroud)

ale*_*zel 14

在VueJs 2.1.3版中,您可以使用:

父:

<div v-for="row in rows">
    <slot name="buttons" :row="row"></slot>
</div>
Run Code Online (Sandbox Code Playgroud)

儿童:

<template slot="buttons" scope="props">
    <a href="props.row.href">go there</a>
</template>
Run Code Online (Sandbox Code Playgroud)

这种结构不会产生任何警告,因此我认为它是有效的.

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


ier*_*dna 3

在 VueJS 1.0.16 中,你可以在模板中执行此操作:

<div v-for="item in tiems">
    <slot :name="item.id"></slot>
</div>
Run Code Online (Sandbox Code Playgroud)

然而,从 1.0.17 开始,VueJS 抛出了这个错误:<slot :name="item.id">: slot names cannot be dynamic.

  • 哈哈,我只是重新设计了一个组件来适应这个......我当然希望他不会在 1.0.19 中再次改变主意 (2认同)