首页 > 其他分享 >Vue之插槽

Vue之插槽

时间:2022-12-26 23:22:19浏览次数:46  
标签:Category Vue name 插槽 width export 组件

插槽

  1. 作用:让父组件可以向子组件指定位置插入html结构,也是一种组件间通信的方式,适用于 父组件 ===> 子组件 。

  2. 分类:默认插槽、具名插槽、作用域插槽

  3. 使用方式:

   默认插槽

父组件中:
        <Category>
           <div>html结构1</div>
        </Category>
子组件中:
        <template>
            <div>
               <!-- 定义插槽 -->
               <slot>插槽默认内容...</slot>
            </div>
        </template>

示例:

Category.vue

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

<script>
    export default {
        name:'Category',
        props:['title']
    }
</script>

<style scoped>
    .category{
        background-color: skyblue;
        width: 200px;
        height: 300px;
    }
    h3{
        text-align: center;
        background-color: orange;
    }
    video{
        width: 100%;
    }
    img{
        width: 100%;
    }
</style>

 

App.vue

<template>
    <div class="container">
        <Category title="美食" >
            <img src="https://s3.ax1x.com/2021/01/16/srJlq0.jpg" alt="">
        </Category>

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

        <Category title="电影">
            <video controls src="http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"></video>
        </Category>
    </div>
</template>

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

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

 

其中红色部分是解析完成之后传到Category组件中的:

 

 结果:

 

 

具名插槽

父组件中:
        <Category>
            <template slot="center">
              <div>html结构1</div>
            </template>

            <template v-slot:footer>
               <div>html结构2</div>
            </template>
        </Category>
子组件中:
        <template>
            <div>
               <!-- 定义插槽 -->
               <slot name="center">插槽默认内容...</slot>
               <slot name="footer">插槽默认内容...</slot>
            </div>
        </template>

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:'Category',
        props:['title']
    }
</script>

<style scoped>
    .category{
        background-color: skyblue;
        width: 200px;
        height: 300px;
    }
    h3{
        text-align: center;
        background-color: orange;
    }
    video{
        width: 100%;
    }
    img{
        width: 100%;
    }
</style>

 

App.vue

<template>
    <div class="container">
        <Category title="美食" >
            <img slot="center" src="https://s3.ax1x.com/2021/01/16/srJlq0.jpg" alt="">
            <a slot="footer" href="http://www.atguigu.com">更多美食</a>
        </Category>

        <Category title="游戏" >
            <ul slot="center">
                <li v-for="(g,index) in games" :key="index">{{g}}</li>
            </ul>
            <div class="foot" slot="footer">
                <a href="http://www.atguigu.com">单机游戏</a>
                <a href="http://www.atguigu.com">网络游戏</a>
            </div>
        </Category>

        <Category title="电影">
            <video slot="center" controls src="http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"></video>
            <template v-slot:footer>
                <div class="foot">
                    <a href="http://www.atguigu.com">经典</a>
                    <a href="http://www.atguigu.com">热门</a>
                    <a href="http://www.atguigu.com">推荐</a>
                </div>
                <h4>欢迎前来观影</h4>
            </template>
        </Category>
    </div>
</template>

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

<style scoped>
    .container,.foot{
        display: flex;
        justify-content: space-around;
    }
    h4{
        text-align: center;
    }
</style>

 

 

 

         作用域插槽

    1. 理解:数据在组件的自身,但根据数据生成的结构需要组件的使用者来决定。(games数据在Category组件中,但使用数据所遍历出来的结构由App组件决定)

    2. 具体编码:

      父组件中:
      		<Category>
      			<template scope="scopeData">
      				<!-- 生成的是ul列表 -->
      				<ul>
      					<li v-for="g in scopeData.games" :key="g">{{g}}</li>
      				</ul>
      			</template>
      		</Category>
      
      		<Category>
      			<template slot-scope="scopeData">
      				<!-- 生成的是h4标题 -->
      				<h4 v-for="g in scopeData.games" :key="g">{{g}}</h4>
      			</template>
      		</Category>
      子组件中:
              <template>
                  <div>
                      <slot :games="games"></slot>
                  </div>
              </template>
      		
              <script>
                  export default {
                      name:'Category',
                      props:['title'],
                      //数据在子组件自身
                      data() {
                          return {
                              games:['红色警戒','穿越火线','劲舞团','超级玛丽']
                          }
                      },
                  }
              </script>

示例:

Category.vue

<template>
    <div class="category">
        <h3>{{title}}分类</h3>
        <!-- 往组件的使用者传递二个属性信息,分别是games和msg,其中:games后面的双引号内部是表达式 -->
        <slot :games="games" msg="hello">我是默认的一些内容</slot>
    </div>
</template>

<script>
    export default {
        name:'Category',
        props:['title'],
        data() {
            return {
                games:['红色警戒','穿越火线','劲舞团','超级玛丽'],
            }
        },
    }
</script>

<style scoped>
    .category{
        background-color: skyblue;
        width: 200px;
        height: 300px;
    }
    h3{
        text-align: center;
        background-color: orange;
    }
    video{
        width: 100%;
    }
    img{
        width: 100%;
    }
</style>

 

App.vue:

<template>
    <div class="container">

        <Category title="游戏">
            <!-- 要结合template标签以及属性scope来使用得到子组件的数据 -->
            <template scope="atguigu">
                <ul>
                    <li v-for="(g,index) in atguigu.games" :key="index">{{g}}</li>
                </ul>
            </template>
        </Category>

        <Category title="游戏">
            <!-- 第二种写法,可以直接拿到子组件传来的其中一个属性 -->
            <template scope="{games}">
                <ol>
                    <li style="color:red" v-for="(g,index) in games" :key="index">{{g}}</li>
                </ol>
            </template>
        </Category>

        <Category title="游戏">
            <!-- 第三种写法 -->
            <template slot-scope="{games}">
                <h4 v-for="(g,index) in games" :key="index">{{g}}</h4>
            </template>
        </Category>

    </div>
</template>

<script>
    import Category from './components/Category'
    export default {
        name:'App',
        components:{Category},
    }
</script>

<style scoped>
    .container,.foot{
        display: flex;
        justify-content: space-around;
    }
    h4{
        text-align: center;
    }
</style>

 

 

<template>     <div class="container">         <Category title="美食" >             <img src="https://s3.ax1x.com/2021/01/16/srJlq0.jpg" alt="">         </Category>
        <Category title="游戏" >             <ul>                 <li v-for="(g,index) in games" :key="index">{{g}}</li>             </ul>         </Category>
        <Category title="电影">             <video controls src="http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"></video>         </Category>     </div> </template>
<script>     import Category from './components/Category'     export default {         name:'App',         components:{Category},         data() {             return {                 foods:['火锅','烧烤','小龙虾','牛排'],                 games:['红色警戒','穿越火线','劲舞团','超级玛丽'],                 films:['《教父》','《拆弹专家》','《你好,李焕英》','《尚硅谷》']             }         },     } </script>
<style scoped>     .container{         display: flex;         justify-content: space-around;     } </style>

标签:Category,Vue,name,插槽,width,export,组件
From: https://www.cnblogs.com/anjingdian/p/17007131.html

相关文章

  • vue3_01生命周期函数
    <template><div><p>这是第一个组件</p></div></template><scriptlang="ts">import{defineComponent,onBeforeMount,onMounted}fr......
  • Vue之mixin混合
    mixin(混入)1.功能:可以把多个组件共用的配置提取成一个混入对象2.使用方式:  第一步定义混合:  ```  {    data(){....},    methods......
  • Vue之props
     props配置项1.功能:让组件接收外部传过来的数据2.传递数据:```<Demoname="xxx"/>```3.接收数据:  1.第一种方式(只接收):```props:['name']```  2.第二......
  • Vue之ref
    ref属性1.被用来给元素或子组件注册引用信息(id的替代者)2.应用在html标签上获取的是真实DOM元素,应用在组件标签上是组件实例对象(vc)3.使用方式:  1.打标识:```<h1......
  • 小记Vue动态修改表头的方法
    背景:列表A:初始列名称列表对象B:{name1:newName1;name2:newName2}对象B记录了一部分需要修改的列名称。根据列表A使用v-for动态渲染......
  • 记录--vue.config.js 的完整配置(超详细)!
    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助前段时间,对部门的个别项目进行Vue3.0+ts框架的迁移,刚开始研究的时候也是踩坑特别多,尤其我们的项目还有些......
  • Vue2.00.WebPack
    本次系统学习以黑码教程为主,后期随笔中不特殊说明均为黑马教程内容01.对webpack的了解a.webpack是前端项目工程化的具体解决方案;b.主要功能:1.提......
  • VueX基础篇
    1.VueX是什么:“Vuex是一个专为Vue.js应用程序开发的状态管理模式。它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化。”......
  • 关于优化前端vue项目的思考
    背景:前端项目 125M启动时间5-10分钟F12会出现崩溃原因:打包出来的css样式文件体积过大解决办法:1.启动太久,将vue-cli升级到版本5,能优化不少的时间2.vue.conf......
  • vue 多项目nginx部署---二级目录
    vue多项目部署---二级目录新项目:同一域名下部署多个vue项目,根目录vue项目不需要按下面的方式进行打包部署https://www.bitedit.com/下面三个项目以二级目录部署htt......