美文网首页
Vue插槽slot的用法

Vue插槽slot的用法

作者: 扣丁李 | 来源:发表于2021-09-28 02:43 被阅读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

    相关文章

      网友评论

          本文标题:Vue插槽slot的用法

          本文链接:https://www.haomeiwen.com/subject/gkrlnltx.html