vue中的三种插槽类型

插槽的作用:

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

一、默认插槽

使用方法:

①、在父组件中,将子组件标签写成双节点形式,在中间写上要传入的标签内容

②、在子组件中,使用<slot></slot>标签进行占位,等待使用者在父组件的子组件标签中编写代码,编写的代码内容就会填充到slot标签所处的位置

子组件(Category.vue)代码:

<template>
  <div class="category">
    <h3>{
   
   {title}}分类</h3>
    <!-- 定义一个默认插槽,等待使用者编写标签进行填充 -->
    <slot>如果没有设置插入的标签,我就会出现</slot>
  </div>
</template>

父组件(App.vue)代码:

<template>
    <div class="container">
        <!-- 将子组件标签写成双节点形式,在中间写上要传入的标签内容 -->
        <Category title="美食">
            <img src="https://s3.ax1x.com/2021/01/16/srJ1q0.jpg" alt="">
        </Category>
    </div>
</template>

二、具名插槽

使用方法:

①、在子组件中,为slot标签设置name属性,属性名为插槽的名字,便于父组件将各个标签定位在指定的位置

①、在父组件中,为子组件标签中的标签设置slot属性能够但只能够为其中的template标签设置v-slot属性

Category.vue代码:

<template>
  <div class="category">
    <h3>{
   
   {title}}分类</h3>
    <!-- 具名插槽 -->
    <slot name="center">如果没有设置插入的标签,我就会出现1</slot>
    <slot name="footer">如果没有设置插入的标签,我就会出现2</slot>
  </div>
</template>

App.vue代码:

<template>
    <div class="container">
        <!-- 将组件标签写成双节点形式,在中间写上要传入的标签内容 -->
        <Category title="美食">
            <img slot="center" src="https://s3.ax1x.com/2021/01/16/srJ1q0.jpg" alt="">
            <a slot="footer" href="http://www.baidu.com">更多美食</a>
        </Category>

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

        <Category title="电影">
            <video slot="center" controls src="http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"></video>
            <!-- v-slot属性只可以在template标签使用 -->
            <template v-slot:footer>
                <div class="foot">
                    <a href="http://www.baidu.com">经典</a>
                    <a href="http://www.baidu.com">热门</a>
                    <a href="http://www.baidu.com">推荐</a>
                </div>
                <h4>欢迎前来观影</h4>
            </template>
        </Category>
    </div>
</template>

三、作用域插槽

作用:

数据子组件自身,但是根据数据父组件中生成的结构需要父组件的使用者来决定;

Category.vue代码:

<template>
  <div class="category">
    <h3>{
   
   {title}}分类</h3>
    <!-- 将games数据通过传参的方式传递给App -->
    <slot :games="games"></slot>
  </div>
</template>

<script>
export default {
    name:'Category',
    props:['title'],
    data() {
        return {
            games:['王者荣耀','糖豆人','穿越火线']
        }
    }
}
</script>

App.vue代码:

<template>
    <div class="container">
        <!-- 实现根据用户自定义标签 -->
        <!-- 使用scope属性,属性值可以与组件插槽的属性名不同 -->
        <Category title="游戏">
            <!-- 一定要使用template标签包裹内容 -->
            <template scope="atguigu">
                <ul>
                    <li v-for="(item,index) in atguigu.games" :key="index">{
   
   {item}}</li>
                </ul>
            </template>
        </Category>

         <Category title="游戏">
            <template scope="atguigu">
                <ol>
                    <li v-for="(item,index) in atguigu.games" :key="index">{
   
   {item}}</li>
                </ol>
            </template>
        </Category>

        <Category title="游戏">
            <template scope="atguigu">
                <h4 v-for="(item,index) in atguigu.games" :key="index">{
   
   {item}}</h4>
            </template>
        </Category>
    </div>
</template>

猜你喜欢

转载自blog.csdn.net/weixin_46376652/article/details/125858200