VUE中插槽slot用法 及其使用场景

什么是插槽?

我们知道,在vue中,引入的子组件标签中间是不允许写内容的。为了解决这个问题,官方引入了插槽(slot)的概念。

插槽,其实就相当于占位符。它在组件中给你的HTML模板占了一个位置,让你来传入一些东西。插槽又分为匿名插槽具名插槽以及作用域插槽

你可能不太明白,为什么我要给子组件中传入HTML,而不直接写在子组件中呢?答案是这样的。你可以想象一个场景,你有五个页面,这五个页面中只有一个区域的内容不一样,你会怎么去写这五个页面呢?复制粘贴是一种办法,但在vue中,插槽(slot)是更好的做法。
在这里插入图片描述

匿名插槽


匿名插槽,我们又可以叫它单个插槽或者默认插槽。与具名插槽相对,它不需要设置name属性。(它隐藏的name属性为default。)

例子:

文件目录如下,Home组件是HelloWorld的父组件。在这里插入图片描述

  • 在HelloWorld中写一个匿名插槽
<template>
  <div class="hello">
     Helloworld组件

     <div class = 'slotTxt'>
       <slot></slot>
     </div>

  </div>
</template>

<script>
export default {

}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="less">
.hello{
  width:100%;
  height:300px;
  background:#ccc;
  margin-top:50px;
  .slotTxt{
    width:500px;
    height:200px;
    margin:30px auto;
    background:red;
  }
}
</style>

 

  • 在Home组件中引入子组件,并在子组件标签中写入内容
<template>
  <div class="home">
    我是Home父组件
    <HelloWorld>
      <!-- 没有插槽,这里的内容不显示 -->
      <h1>我是helloworld中的插槽啊</h1>  
    </HelloWorld>
  </div>
</template>

<script>
import HelloWorld from '@/components/HelloWorld.vue'
export default {
  name: 'home',
  components: {
    HelloWorld
  }
}
</script>

 

  • 效果
    在这里插入图片描述
    不难看出,HelloWorld标签中的内容(红色部分)已经显示出来了。

具名插槽


上面已经说过,插槽有一个name属性。与匿名插槽相对,加了name属性的匿名插槽就是具名插槽。

  • HelloWorld组件中写入name属性分别为left和right的插槽
  • <template>
      <div class="hello">
         Helloworld组件
    
         <div class = 'slotLeft'>
           <slot name='left'></slot>
         </div>
    
         <div class = 'slotRight'>
           <slot name='right'></slot>
         </div>
    
      </div>
    </template>
    
    <script>
    export default {
    
    }
    </script>
    
    <!-- Add "scoped" attribute to limit CSS to this component only -->
    <style scoped lang="less">
    .hello{
      width:700px;
      height:300px;
      background:#ccc;
      margin: 0 auto;
      margin-top:50px;
      .slotLeft{
        width:300px;
        height:200px;
        float:left;
        background:red;
      }
      .slotRight{
        width:300px;
        height:200px;
        float:right;
        background:pink;
      }
    }
    </style>
    
    
  • Home组件通过在template上写v-slot:name来使用具名插槽
<template>
  <div class="home">
    我是Home父组件
    <HelloWorld>
      <template v-slot:left>
         <h1>name属性为left</h1> 
      </template>
      <template v-slot:right>
         <h1>name属性为right</h1> 
      </template>
     
    </HelloWorld>
  </div>
</template>

<script>
import HelloWorld from '@/components/HelloWorld.vue'
export default {
  name: 'home',
  components: {
    HelloWorld
  }
}
</script>
<style lang="less" scoped>
.home{
  width:900px;
  margin:0 auto;
  background:yellow;
  padding-bottom:100px;
}
</style>

注意 v-slot 只能添加在template标签上 (只有一种例外情况)。

  • 效果
    在这里插入图片描述
  • 例外情况(被废弃的slot=‘name’)
    带slot属性的具名插槽自 2.6.0 起被废弃,vue3.x被完全废弃。只有vue3之前的cli可以使用。
<template>
  <div class="home">
    我是Home父组件
    <HelloWorld>
      <h1 slot='left'>name属性为left</h1>  
      <h1 slot='right'>name属性为right</h1>  
    </HelloWorld>
  </div>
</template>

<script>
import HelloWorld from '@/components/HelloWorld.vue'
export default {
  name: 'home',
  components: {
    HelloWorld
  }
}
</script>
<style lang="less" scoped>
.home{
  width:900px;
  margin:0 auto;
  background:yellow;
  padding-bottom:100px;
}
</style>

效果同上。

  • 具名插槽的小知识点
    跟 v-on 和 v-bind 一样,v-slot 也有缩写,即把参数之前的所有内容 (v-slot:) 替换为字符 #。例如 v-slot:header 可以被重写为 #header。

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue插槽的实际应用场景非常广泛。下面列举一些常见的应用场景: 1. 组件间的内容传递:通过插槽,可以在父组件向子组件传递内容,子组件可以接收并渲染这些内容。这样可以使得组件更加灵活和可复用。 2. 组件的布局控制:通过插槽,可以在父组件控制子组件的布局。父组件可以在组件标签内部插入HTML元素或者其他组件,从而实现对子组件的布局定制。 3. 列表渲染:通过插槽,可以在父组件定义列表项的模板,并将数据传递给子组件进行渲染。这样可以实现灵活的列表渲染,并且每个列表项的模板可以根据需求进行定制。 4. 多个插槽使用:在一个组件可以定义多个插槽,通过插槽名来区分不同的插槽。这样可以在父组件根据需要插入不同的内容,并在子组件将这些内容进行渲染。 综上所述,Vue插槽可以用于组件间的内容传递、布局控制、列表渲染以及多个插槽使用场景,使得组件的灵活性和复用性大大提高。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [VUE插槽slot用法 及其使用场景](https://blog.csdn.net/xifanxiaochaorou/article/details/123554221)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值