Vue插槽slot的用法

2021-09-28  本文已影响0人  扣丁李

子组件

<template>
    <div class="card-box">
        <div class="card-head">
            <slot name="card-head"></slot>
        </div>
        <div class="card-body">
            <slot></slot>
        </div>
    </div>
</template>

<script>
</script>

<style scoped="scoped">
    .card-box{
        border: 1px solid cornflowerblue;
        border-radius: 2px;
        width: 400px;
        min-height: 200px;
    }
    .card-head{
        height: 50px;
        line-height: 50px;
        text-align: left;
        padding: 0 5px;
        color: white;
        font-size: 18px;
        background-color: cornflowerblue;
    }
    .card-body{
        text-align: left;
        padding: 0 5px;
    }
</style>

父组件

<template>
    <div id="app">
        <Card>
            <template slot="card-head">头部</template>
            我是内容
        </Card>
    </div>
</template>

<script>
    import Card from './components/Card.vue'

    export default {
        name: 'app',
        components: {
            Card
        },
        data(){
            return{
        
            }
        },
        methods:{
            
        }
    }
</script>

<style>
    #app {
        font-family: 'Avenir', Helvetica, Arial, sans-serif;
        -webkit-font-smoothing: antialiased;
        -moz-osx-font-smoothing: grayscale;
        text-align: center;
        color: #2c3e50;
        margin-top: 60px;
    }
</style>

效果

image.png
上一篇下一篇

猜你喜欢

热点阅读