笔记

具名插槽

当年挖坑(子组件)的时候,给这个坑取了一个名,等填坑(父组件)的时候,说明白(把哪个结构填到哪个结构里)就中了。

重要文件源码

App.vue

<template>
    <div class="container">
        <!--
        <Category title="美食" :listData="foods"/>
        <Category title="游戏" :listData="games"/>
        <Category title="电影" :listData="films"/>
        -->
        <Category title="美食">
            <!-- 由于此时是在 App.vue 组件中,所以当前模板解析完成之后,才放到了 Category组件中。 
                 那么想控制img样式,就可以在当前组件写样式。
             -->
            <img slot="center" src="https://imgchr.com/content/images/system/home_cover_1601010270144_8921bc.jpg" alt="">

            <a slot="footer" href="https://softool.cn">更多美食</a>
        </Category>

        <Category title="游戏">
            <ul slot="center">
                <li v-for="(g,index) in games" :key="index">{{ g }}</li>
            </ul>

            <!--a slot="footer" href="https://softool.cn">单机游戏</a>
            <a slot="footer" href="https://softool.cn">网络游戏</a-->
            <div class="foot" slot="footer">
                <a href="https://softool.cn">单机游戏</a>
                <a href="https://softool.cn">网络游戏</a>
            </div>
        </Category>

        <Category title="电影">
            <!-- controls 播放 -->
            <video slot="center" src="https://cdn.pixabay.com/video/2022/11/03/137614-767056227_large.mp4"></video>

            <!--
            <div class="foot" slot="footer">
                <a href="https://softool.cn">经典</a>
                <a href="https://softool.cn">热门</a>
                <a href="https://softool.cn">推荐</a>
            </div>
            <h4 slot="footer">欢迎前来观影</h4>
            -->

            <!--
                <div slot="footer">
                    <div class="foot">
                        <a href="https://softool.cn">经典</a>
                        <a href="https://softool.cn">热门</a>
                        <a href="https://softool.cn">推荐</a>
                    </div>
                    <h4>欢迎前来观影</h4>
                </div>
            -->
            <!-- 为了减少一层div,改用 template(可以包裹元素,但是不会生成元素) 这样可以省一层结构
            <template slot="footer">
                <div class="foot">
                    <a href="https://softool.cn">经典</a>
                    <a href="https://softool.cn">热门</a>
                    <a href="https://softool.cn">推荐</a>
                </div>
                <h4>欢迎前来观影</h4>
            </template>
            -->
            <!-- template 的第2种写法: Vue2.6新提出来的  v-slot:槽名  这种写法只能写在 template结构 中。 -->
            <template v-slot:footer>
                <div class="foot">
                    <a href="https://softool.cn">经典</a>
                    <a href="https://softool.cn">热门</a>
                    <a href="https://softool.cn">推荐</a>
                </div>
                <h4>欢迎前来观影</h4>
            </template>

        </Category>
    </div>
</template>

<script>
    //1. 引入组件
    import Category from './components/Category.vue'



    export default {
        name:'App',
        //2. 注册组件
        components: { Category },

        data(){
            return{
                foods: ['火锅', '烧烤', '小龙虾', '牛排'],
                games: ['红色警戒', '穿越火线', '劲舞团', '超级玛丽'],
                films: ['《教父》', '《拆弹专家》', '《你好,李焕英》', '《尚硅谷》'],
            }
        }

    }
</script>


<style>
    .container, .foot{
        display: flex;
        justify-content: space-around;
    }

    img{
        width: 100%;
    }
    video{
        width: 100%;
    }


</style>

components\Category.vue

<template>
    <div class="category">
        <h3>{{ title }} 分类</h3>
        <!-- 定义一个插槽(挖个坑,等着组件的使用者进行填充)
        插槽可以写多个,但是必须有一个名字。
        -->
        <slot name="center">我是一个默认值,当使用者没有传递具体结构时,我会出现1</slot>

        <slot name="footer">我是一个默认值,当使用者没有传递具体结构时,我会出现2</slot>
    </div>
</template>

<script>
    export default {
        name:'CategoryVue',

        props:[title']
    }
</script>

<style>
    .category{
        background-color: skyblue;
        width: 200px;
        height: 300px;
    }

    h3{
        text-align: center;
        background-color: orange;
    }
    /*
    img{
        width: 100%;
    }
    */
</style>