Vue中的slot(占坑,预留位置)

Vue中的slot(占坑,预留位置)

  1. 子模板不使用slot
  2. 子模板使用slot
  3. 子模板使用使用name属性,且传递data
  • 文件名:Slots.vue
//slot组件
<template>
  <div class="Slots">
      <div class="myd">
      在子组件中不使用slot时SlotChildwithnoslots下的内容不会显示
      <SlotChildwithnoslots>         
          <div class="no-name">111111111111111111我是嵌在子组件内不具有属性名的标签</div>
          <div >2222222222222222222222222222我是嵌在子组件内不具有属性名的标签</div>
      </SlotChildwithnoslots>
    </div>
      '
      '
      '
      '
      '
      '
      '
      <div class="myd">'
      在子组件中使用slot时SlotChildwithslots下的内容会显示
       <SlotChildwithslots>         
          <div class="no-name">111111111111111111我是嵌在子组件内不具有属性名的标签</div>
          <div >2222222222222222222222222222我是嵌在子组件内不具有属性名的标签</div>
      </SlotChildwithslots>
      </div>  
        '
      '
      '
      '
      '
      '
      '.
       <div class="myd">'
      在子组件中使用slot时且包含数据时下的内容会显示
       <SlotChildwithslotsanddata>         
          <div slot="header">111111111111111111我是嵌在子组件内不具有属性名的标签</div>
          <div slot="header">2222222222222222222222222222我是嵌在子组件内不具有属性名的标签</div>

 

          <template slot-scope="user">
                <div v-for="item in user.data" :key="item.id">
                {{item}}
                </div>
            </template>
      </SlotChildwithslotsanddata>
      </div>  
       



  </div>



</template>

 

<script>
import SlotChildwithnoslots from '@/components/SlotChildwithnoslots'
import SlotChildwithslots from '@/components/SlotChildwithslots'
import SlotChildwithslotsanddata from '@/components/SlotChildwithslotsanddata'

 

export default {
    name: 'Slots',
    components:{
        SlotChildwithnoslots,
        SlotChildwithslots,
        SlotChildwithslotsanddata
    },
    data () {
        return {
      
        }
    }
}
</script>
<style>

 

.myd
background-color:yellow;
height: 300px;
border:1px solid red;
}
</style>
 
  •  文件名:SlotChildwithslots.vue
//slot的子组件
<template>
  <div class="SlotChildwithslots">
      <slot></slot>
      我是slot的子组件      
  </div>
</template>

<script>
export default {
  name: 'SlotChildwithslots',
  data () {
    return {
      
    }
  }
}
</script>
  • 文件名:SlotChildwithnoslots.vue
//slot的子组件
<template>
  <div class="SlotChildwithnoslots">
      我是slot的子组件      
  </div>
</template>

<script>
export default {
  name: 'SlotChildwithnoslots',
  data () {
    return {
      
    }
  }
}
</script>
  • 文件名:SlotChildwithslotsanddata.vue
//slot的子组件
<template>
  <div class="SlotChildwithslotsanddata">
      <slot name="header" ></slot>
      

      我是slot的子组件   
       <slot :data="user"></slot>   
  </div>
</template>

<script>
export default {
  name: 'SlotChildwithslotsanddata',
  data () {
    return {
      user: [
        {name: 'Jack', sex: 'boy'},
        {name: 'Jone', sex: 'girl'},
        {name: 'Tom', sex: 'boy'}
      ]
    }
  }
}
</script>
 

转载于:https://www.cnblogs.com/li9club/p/11522087.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue的插槽是一种用于在组件预留代码空间的技术。其,匿名插槽是一种没有命名的代码空间,可以在组件定义,并在使用组件时直接替换为相应的内容。匿名插槽可以通过在组件内部使用`<slot></slot>`标签来定义和使用。 匿名插槽的使用示例如下: ```html <template> <div> <h1>插槽示例</h1> <slot></slot> </div> </template> ``` 在上述示例,`<slot></slot>`标签表示一个匿名插槽,它在组件定义了一个预留的代码空间。当使用这个组件时,可以在组件标签内部添加内容,这些内容将会替换掉匿名插槽的位置。例如: ```html <template> <div> <h1>插槽示例</h1> <slot></slot> <p>这是插槽下面的内容。</p> </div> </template> ``` 在这个示例,`<slot></slot>`标签所占的位置将被替换为插槽标签内部的内容。 需要注意的是,匿名插槽的样式不能通过在`<slot>`标签上添加样式来进行设置。另外,具有命名的插槽不能被不含`slot`属性的标签内容替换,会显示具名插槽的默认值。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [vue:匿名slot、具名slot、作用域slot(技术栈Vue3 + TS)](https://blog.csdn.net/snowball_li/article/details/123298575)[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: 50%"] - *2* [vue插槽slot的简单理解与用法实例分析](https://download.csdn.net/download/weixin_38690545/14901358)[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: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值