当前位置: 首页 > news >正文

vue中的插槽slot

vue中的插槽slot

  • 1、作用
  • 2、插槽内心
    • 2.1、默认插槽
    • 2.2、具名插槽(命名插槽)
    • 2.3、作用域插槽
      • 2.3.1、实现原理

1、作用

  • 父组件向子组件传递内容
  • 扩展、复用、定制组件

2、插槽内心

2.1、默认插槽

  • 把父组件中的数组,显示在子组件中,子组件通过一个slot插槽标签显示父组件中的数据。

子组件

<template>
  <div class="slotChild">
    <h4>{{msg}}</h4>
    <slot>这是子组件插槽默认的值</slot>
  </div>
</template>

<script>
export default {
  name: "slotChild",
  data() {
    return {
      msg: "vue中的插槽---子组件"
    }
  }
}
</script>

父组件

<template>
  <div class="slotStudy">
    <h1>{{ msg }}</h1>
    <SlotChild>
      <p>这是父组件传入的值,将会替换子组件中slot中编写的默认值</p>
    </SlotChild>
  </div>
</template>

<script>
import SlotChild from "@/components/slot/SlotChild";

export default {
  name: "slotStudy",
  components: {SlotChild},
  data() {
    return {
      msg: "vue中的插槽---父组件"
    }
  }
}
</script>

在这里插入图片描述

    <SlotChild></SlotChild>

在这里插入图片描述

2.2、具名插槽(命名插槽)

  • 父组件中通过slot属性,给插槽命名,在子组件中通过slot标签,根据定义好的名字填充到对应的位置。这样就可以指定多个可区分的slot,在使用组件时灵活的进行插值。

子组件:

<template>
  <div class="slotChild">
    <h4>{{msg}}</h4>
    <h1><slot name="h_1"></slot></h1>
    <h2><slot name="h_2"></slot></h2>
    <h3><slot name="h_3"></slot></h3>
  </div>
</template>

<script>
export default {
  name: "slotChild",
  data() {
    return {
      msg: "vue中的插槽---子组件"
    }
  }
}
</script>

父组件:

<template>
  <div class="slotStudy">
    <h1>{{ msg }}</h1>
    <SlotChild>
      <template v-slot:h_1>h1111111111的内容</template>
      <!--      #简写-->
      <template #h_2>h2222222的内容</template>
      <template v-slot:h_3>h33333的内容</template>
    </SlotChild>
  </div>
</template>

<script>
import SlotChild from "@/components/slot/SlotChild";

export default {
  name: "slotStudy",
  components: {SlotChild},
  data() {
    return {
      msg: "vue中的插槽---父组件"
    }
  }
}
</script>

在这里插入图片描述

2.3、作用域插槽

用得不多。

将子组件中data的数据传出,在父组件中使用。子组件渲染作用域插槽时,可以将子组件内部的数据传递给父组件,让父组件根据子组件的传递过来的数据决定如何渲染该插槽。在标签中通过v-slot="要传过来的数据"来接收数据。

2.3.1、实现原理

实现原理:当子组件vm实例化时,获取到父组件传入的slot标签的内容,存放在vm. s l o t 中,默认插槽为 v m . slot中,默认插槽为vm. slot中,默认插槽为vm.slot.default,具名插槽为vm. s l o t . x x x , x x x 为插槽名,当组件执行渲染函数时候,遇到 s l o t 标签,使用 slot.xxx,xxx 为插槽名,当组件执行渲染函数时候,遇到slot标签,使用 slot.xxxxxx为插槽名,当组件执行渲染函数时候,遇到slot标签,使用slot中的内容进行替换,此时可以为插槽传递数据,若存在数据,则可称该插槽为作用域插槽。

子组件:

<template>
  <div class="slotChild">
    <h4>{{ msg }}</h4>
    <h1>
      <slot :str="strDate" name="n_str">{{ strDate.name }}</slot>
    </h1>
    <h2>
      <slot :str="strDate" name="j_str">{{ strDate.job }}</slot>
    </h2>

  </div>
</template>

<script>
export default {
  name: "slotChild",
  data() {
    return {
      msg: "vue中的插槽---子组件",
      strDate: {
        name: "学习前端的小方同学",
        job: "找工作中",
        age:"我每年都是18"
      }
    }
  }
}
</script>

父组件:

<template>
  <div class="slotStudy">
    <h1>{{ msg }}</h1>
    <SlotChild>
      <template #n_str="strProps">
        {{ strProps.str.job }}
      </template>
      <template v-slot:j_str="strProps">
        {{ strProps.str.age }}
      </template>
    </SlotChild>
  </div>
</template>

<script>
import SlotChild from "@/components/slot/SlotChild";

export default {
  name: "slotStudy",
  components: {SlotChild},
  data() {
    return {
      msg: "vue中的插槽---父组件"
    }
  }
}
</script>

在这里插入图片描述

相关文章:

  • 搭建Redis主从复制、哨兵模式
  • 深度解析ArrayList使用
  • 吴恩达深度学习笔记(六)——超参数调试、Batch正则化和程序框架
  • 【甄选靶场】Vulnhub百个项目渗透——项目十六:FristiLeaks_1.3(文件上传,py脚本改写,sudo提权,脏牛提权,源码获取)
  • 苹果iPhone手机iOS16如何取消关闭复制粘贴时不停弹出的剪贴板粘贴提示通知弹窗?
  • Android移动应用开发之ImageView、ProgressBar和Notification的一些简单使用
  • SQL经典练习题(openGauss数据库)上
  • CSDN编程竞赛-第六期(下)
  • Spring 静态属性赋值 @value 注入静态属性 @Value注解用法
  • 【零基础学QT】第九章 窗口美化QSS的使用
  • aws codesuit workshop
  • Linux内存管理——段页式访问
  • Hadoop基础学习笔记
  • 微信小程序 - 页面插入添加 Banner 广告超详细教程(支持自定义样式、位置、大小等)及注意事项
  • JavaScript笔记9-节点操作
  • “Material Design”设计规范在 ComponentOne For WinForm 的全新尝试!
  • 【翻译】Mashape是如何管理15000个API和微服务的(三)
  • 3.7、@ResponseBody 和 @RestController
  • django开发-定时任务的使用
  • ECMAScript入门(七)--Module语法
  • electron原来这么简单----打包你的react、VUE桌面应用程序
  • extract-text-webpack-plugin用法
  • Terraform入门 - 3. 变更基础设施
  • WordPress 获取当前文章下的所有附件/获取指定ID文章的附件(图片、文件、视频)...
  • Xmanager 远程桌面 CentOS 7
  • 读懂package.json -- 依赖管理
  • 浮动相关
  • 微信小程序设置上一页数据
  • 微信小程序填坑清单
  • 问:在指定的JSON数据中(最外层是数组)根据指定条件拿到匹配到的结果
  • 用Node EJS写一个爬虫脚本每天定时给心爱的她发一封暖心邮件
  • 自动记录MySQL慢查询快照脚本
  • ​Java基础复习笔记 第16章:网络编程
  • ​软考-高级-系统架构设计师教程(清华第2版)【第12章 信息系统架构设计理论与实践(P420~465)-思维导图】​
  • #Spring-boot高级
  • #设计模式#4.6 Flyweight(享元) 对象结构型模式
  • #我与Java虚拟机的故事#连载05:Java虚拟机的修炼之道
  • (28)oracle数据迁移(容器)-部署包资源
  • (CPU/GPU)粒子继承贴图颜色发射
  • (C语言)二分查找 超详细
  • (NSDate) 时间 (time )比较
  • (python)数据结构---字典
  • (超简单)构建高可用网络应用:使用Nginx进行负载均衡与健康检查
  • (附源码)spring boot北京冬奥会志愿者报名系统 毕业设计 150947
  • (附源码)计算机毕业设计SSM在线影视购票系统
  • ****** 二十三 ******、软设笔记【数据库】-数据操作-常用关系操作、关系运算
  • ./configure、make、make install 命令
  • .net 4.0发布后不能正常显示图片问题
  • .net core 连接数据库,通过数据库生成Modell
  • .Net 中的反射(动态创建类型实例) - Part.4(转自http://www.tracefact.net/CLR-and-Framework/Reflection-Part4.aspx)...
  • .NET简谈设计模式之(单件模式)
  • .NET开源快速、强大、免费的电子表格组件
  • ??在JSP中,java和JavaScript如何交互?
  • @Autowired注解的实现原理
  • @cacheable 是否缓存成功_让我们来学习学习SpringCache分布式缓存,为什么用?