详解Vue学习笔记入门篇之组件的内容分发(slot)

2020-06-13 10:43:56易采站长站整理

作用域插槽更具代表性的用例是列表组件,允许组件自定义应该如何渲染列表每一项:


<div id="app">
<my-component :items="items">
<template slot="item" scope="props">
<li>{{props.text}}</li>
</template>
</my-component>
</div>


Vue.component('my-component',{
template:`
<ul>
<slot name="item" v-for="item in items" :text="item.text"></slot>
</ul>
`,
props:['text','items']})
new Vue({
el:'#app',
data:{
items:[
{text:'item1'},
{text:'item2'},
{text:'item3'},
] }
})

作用域插槽也可以是具名的

运行结果: