vue之slot插槽

vue之slot插槽

slot的作用:

假如父组件需要在子组件内放一些DOM元素,默认情况下是不会显示的,如下图所示,页面并没有显示父组件增加的< span>子组件内DOM元素</ span> 元素内容。那么要加DOM元素到子组件上,这就用到了slot插槽,使用slot这个标签可以将父组件放在子组件的内容,放到它想显示的地方

<div>
  <children>  
    <span>子组件内DOM元素</span>  
    <!--上面这行不会显示-->  
  </children>  
</div>  
<script>  
  var vm = new Vue({  
      el: '#app',  
      components: {  
         children: {   
         template: "<h1>显示子组件</h1>"  
       }  
     }  
  });  
</script>  

结果:

显示子组件

用slot显示子组件内DOM元素

<div id="app">  
  <children>  
    <span>子组件内DOM元素</span>  
    <!--上面这行会显示在 “我是子组件” 数据后面-->  
  </children>  
</div>  
<script>  
  var vm = new Vue({  
      el: '#app',  
      components: {  
         children: {   
         template: "<h1>显示子组件</h1><slot></slot>"  
       }  
     }  
  });  
</script>  

结果:

显示子组件
子组件内DOM元素

具名插槽

将父组件添加的HTML标签放在子组件里的不同位置。具名插槽实现:先在子组件对应分发slot标签里,添加name=”name名” 属性,其次父组件在要分发的标签里添加 slot=”name名” 属性,然后就会将对应的标签放在对应的位置了。

简单理解就是:给子组件占的每一个坑取名,将父组件添加的HTML元素添加到指定名字的坑,就实现了分发内容在不同位置显示

【Child组件模板】

<template>
  <div>
    <slot name="header"></slot>
    <h1>子组件</h1>
    <slot name="footer"></slot>
  </div>
</template>

【父组件引用Child组件】

 <Child>
      <span slot="header">header</span>
      <span slot="footer">footer</span>
    </Child>

结果:

header
子组件
footer

编辑作用域

父组件模板的内容在父组件作用域内编译;子组件模板的内容在子组件作用域内编译

【Child组件模板】

<template>
  <div>
    <slot name="header"></slot>
    <h1>{{msg}}</h1>
    <slot></slot>
    <slot name="footer"></slot>
  </div>
</template>
<script>
export default {
  data() {
    return {
      msg: '子组件里面的内容'
    }
  }
}
</script>

【父组件引用Child组件】

<template>
  <Child>
    <span slot="header">header</span>
    <h1>{{msg}}</h1>
    <span slot="footer">footer</span>
  </Child>
</template>
<script>
export default {
  data() {
    return {
      msg: '父组件的内容'
    }
  }
}
</script>

结果:

header
子组件的内容
父组件的内容
footer

解构slot-scope

在子组件中插槽中通过:data绑定了数据,父组件可以通过slot-scope="name"来取得子组件作用域插槽:data绑定的数据,name的名称可以随便取,用来定义对象来代替取到的data数据。

【Child组件模板】

<template>
  <div>
    <slot :data="data"></slot>
  </div>
</template>
<script>
export default {
  data() {
    return {
      data: ['apple','banana','orange','pear']
    }
  }
}
</script>

【父组件引用Child组件】

  <template>
    <!-- 循环数据列表 -->
    <Child>
      <div slot-scope="msg">
        <span v-for="item in msg.data">{{item}} </span>
      </div>
    </Child>
 
    <!-- 直接显示数据 -->
    <Child>
      <div slot-scope="msg">
        <span>{{msg.data}} </span>
      </div>
    </Child>
 
    <!-- 不使用其提供的数据, 作用域插槽退变成匿名插槽 -->
    <Child>
      <div>插槽</div>
    </Child>
  </template>

结果:

apple banana orange pear
['apple','banana','orange','pear']
插槽

参考文章:https://blog.csdn.net/qq_38128179/article/details/85273522

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue中的插槽slot)是一种用于在组件中插入内容的机制。它允许我们在组件的模板中定义一些占位符,然后在使用该组件时,可以将具体的内容插入到这些占位符中。 在Vue 2.x版本中,我们可以使用`<slot>`元素来定义插槽,并使用`<slot>`元素的`name`属性来定义具名插槽。例如: ```html <template> <div> <slot></slot> <!-- 默认插槽 --> <slot name="header"></slot> <!-- 具名插槽 --> <slot name="footer"></slot> <!-- 具名插槽 --> </div> </template> ``` 然后,在使用该组件时,我们可以在组件标签中插入内容,这些内容将会替换对应的插槽。例如: ```html <my-component> <p>默认插槽的内容</p> <template v-slot:header> <h1>具名插槽header的内容</h1> </template> <template v-slot:footer> <p>具名插槽footer的内容</p> </template> </my-component> ``` 在Vue 3.x版本中,为了统一插槽的语法,引入了`v-slot`指令,取代了`slot`和`slot-scope`这两个在Vue 2.x中已被废弃但仍可使用的属性。使用`v-slot`指令时,可以直接在组件标签上使用,而不需要再使用`<template>`元素。例如: ```html <my-component> <template #default> <p>默认插槽的内容</p> </template> <template #header> <h1>具名插槽header的内容</h1> </template> <template #footer> <p>具名插槽footer的内容</p> </template> </my-component> ``` 需要注意的是,在Vue 3.x中,只能使用`v-slot`指令来定义插槽,而不能再使用`slot`和`slot-scope`属性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值