首页 > 其他分享 >slot插槽

slot插槽

时间:2022-11-13 17:55:07浏览次数:38  
标签:slot 插槽 typeslot template 组件 使用者

啥是插槽啊?一句话:让父组件可以向子组件的指定位置插入html结构,也是一种组件间的通信方式.

默认插槽

挖一个坑,等着组件的使用者进行填充

<template>
   <div>
     <h3>{{title}}</h3>
     <!-- 挖个坑 -->
     <slot></slot>
   </div>
</template>
<script>
export default {
   name: 'typeslot',
   props: {
     title:String
   },
};
</script>
<template>
  <typeslot title="美食">
    <img src="../public/img/abe1eea3ca79fc28-c577ebdcb0f3dbcc-257e2ecb7fd1c47961a553ca5ef4505e.jpg" style="width:50px">
  </typeslot>

  <typeslot titke="购物">
    <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
    </ul>
  </typeslot>
  
  <typeslot title="电影">
    <img src="../public/img/abe1eea3ca79fc28-c577ebdcb0f3dbcc-7bcd14d52919cba85df3696608531259.jpg" style="width:50px">
  </typeslot>
</template>

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

结果:

具名插槽

当年挖坑,给每个坑取了个名字,等到填坑,说明白把哪个结构放在哪个坑

子组件:

 <slot name="center"></slot>
 <slot name="footer"></slot>

父组件:

  <typeslot title="美食">
    <template  v-slot:center>
         <!-- 把图片放到main插槽中 -->
       <img src="../public/img/abe1eea3ca79fc28-c577ebdcb0f3dbcc-257e2ecb7fd1c47961a553ca5ef4505e.jpg" style="width:50px">
     </template>

    <template #footer>
        <!-- 把a标签放到footer插槽中 -->
      <a >你好</a>
    </template>
    </typeslot>

最新写法:得使用template包裹 v-slot:名字,不需要双引号;也可以简写为#名字

而且template标签不会变成div元素

结果:

 

 

作用域插槽

数据不在使用者那里,在子组件那边,那么使用者(父组件)怎么展示数据?

作用域插槽使用步骤:

1.在组件那边绑定数据 

2.使用者必须在template中,然后使用v-slot拿到数据,最后运用数据,什么样的形式,什么样的数据展示出来,由使用者绝定.

子组件

  <!-- 绑定数据-->
     <slot :dataList="dataList"></slot>

数据:
data(){
    return{
        dataList:['小猫','小狗','小兔']
    }

父组件:

  <typeslot title="美食">
      <template v-slot="abc">
        <!-- 以无序列表形式展示 -->
          <ul>
            <li v-for="(item,index) in abc.dataList" :key="index">{{item}}</li>
          </ul>
      </template>
    </typeslot>

  <typeslot title="购物">
    <!-- 以有序列表展示 -->
    <template v-slot="abc">
      <ol>
      <li v-for="(item,index) in abc.dataList" :key="index">{{item}}</li>
    </ol>
    </template>
  </typeslot>

结果:

 

补充:

 1.如果使用具名与默认插槽,则需要为默认插槽使用显式的 <template> 标签 写法:#default="a",后面名字自取

2.scope与style里的scoped区别:style标签里的scoped作用的是当前页面的元素

标签:slot,插槽,typeslot,template,组件,使用者
From: https://www.cnblogs.com/pilpill/p/16885878.html

相关文章

  • scrapy SCRAPER_SLOT_MAX_ACTIVE_SIZE
        SCRAPER_SLOT_MAX_ACTIVE_SIZE  SCRAPER_SLOT_MAX_ACTIVE_SIZE:正在处理响应数据的软限制(以字节为单位),如果所有正在处理的响应的大小总和高于此值,Scrapy......
  • ElementUI中Table和Switch组件中插槽scope应用使用场景
    ElementUI中Table和Switch组件中插槽scope应用使用场景ElementUI安装npmielement-ui-S引入Elementmain.js内容添加importElementUIfrom'element-ui';impo......
  • Antd Tree树形控件 自定义插槽
    使用titleRender属性自定义节点渲染函数,不需要处理树型数据,达到比如右侧新增按钮的需求(如图三)<Tree ... titleRender={(nodeData)=>{return(......
  • vue slot的一点理解
    先来一个实例,方便解释:子组件:<template><div><button></button><slot>这里是默认值</slot><slotname="one">这里是默认值one</slot><slotname......
  • 【Vue3】插槽slot
    Vue3插槽父组件向子组件提前挖好的坑(slot)处,填入对应的内容就叫插槽。插入的内容不局限于文本,甚至可以传入多个元素和数组。一、理解插槽如何使用插槽插槽使用的......
  • 学习vue3(五)(插槽slot,Teleport传送组件,keep-alive缓存组件,依赖注入Provide / Inject)
    插槽slot插槽就是子组件中的提供给父组件使用的一个占位符,用<slot></slot>表示,父组件可以在这个占位符中填充任何模板代码,如HTML、组件等,填充的内容会替换子组件的<slot......
  • vue源码分析-插槽原理
    Vue组件的另一个重要概念是插槽,它允许你以一种不同于严格的父子关系的方式组合组件。插槽为你提供了一个将内容放置到新位置或使组件更通用的出口。这一节将围绕官网对插......
  • vue插槽
    1.插槽的用处组件的插槽也是为了让我们封装的组件更加具有扩展性。让使用者可以决定组件内容的一些内容到底展示什么。 2.匿名插槽在子组件中直接使用未标名的......
  • vue计算,监听属性插槽和动态组件
    计算属性如果{{函数()}},每次页面刷新,函数都会重新执行函数---》当属性来使用,缓存<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><ti......
  • Vue表单输入绑定、组件、插槽的使用(三)
    一、表单输入绑定<!DOCTYPEhtml><html> <head> <metacharset="utf-8"> <title></title> <linkrel="stylesheet"href="https://stackpath.bootstrapcdn.com/bo......