Vue slot插槽内容分发——具名插槽

zhuanbike 2022-1-17 699

插槽slot是为了便于开发者专注于前端,更方便的进行开发,插槽虽然不是必用的组件,但是确实能让我们方便不少。

1、在子组件中调用父组件中定义的插槽:

<template>
    <div>
        <div class="header">
            <h1>我是页头标题</h1>
            <div>
                <slot name="header"></slot>
            </div>
        </div>
        <div class="footer">
            <h1>我是页尾标题</h1>
            <div>
                <slot name="footer"></slot>
            </div>
        </div>
    </div>
</template>
 
<script>
    export default {
        name: "child1"
    }
</script>
 
<style scoped>
 
</style>

2、在父组件中定义两个p标签插槽,引入子组件,然后就出现展示效果。

<template>
<div>
    <div>slot内容分发</div>
    <child1>
        <template slot="header">
            <p>我是页头的具体内容</p>
        </template>
        <template slot="footer">
            <p>我是页尾的具体内容</p>
        </template>
    </child1>
</div>
</template>
 
<script>
    import child1 from "./child1.vue";
 
    export default {
        name: "father1",
        components: {
            child1
        }
    }
</script>
 
<style scoped>
 
</style>


最新回复 (0)
发新帖