插槽的使用说明

目录

1.说明

2.分类

3.总结


1.说明

插槽就是子组件中的提供给父组件使用的一个占位符,用<slot></slot> 表示,父组件可以在这个占位符中填充任何模板代码,如 HTML、组件等,填充的内容会替换子组件的<slot></slot>标签,父组件填充的内容称为插槽内容

  • 子组件不提供插槽时,父组件填充失效
  • 父组件无填充时,<slot></slot>中的备用内容会启用生效
  • 父级模板里的所有内容都是在父级作用域中编译的,子模板里的所有内容都是在子作用域中编译的,互不影响

2.分类

(1)默认插槽

①示例

子组件:

<template>
<slot>组件3的默认内容</slot>
</template>

 父组件:

<!-- eslint-disable vue/require-v-for-key -->
<script setup>import Component3 from '@/components/Component3.vue';</script><template><div> <Component3>
</Component3></div>
</template><style scoped>
</style>

结果1:

 

父组件:

<!-- eslint-disable vue/require-v-for-key -->
<script setup>import Component3 from '@/components/Component3.vue';</script><template><div> <Component3><template #default><h1>设置组件3的内容</h1><button>组件三的按钮</button></template>
</Component3></div>
</template><style scoped>
</style>


结果2:

父组件:

<!-- eslint-disable vue/require-v-for-key -->
<script setup>import Component3 from '@/components/Component3.vue';</script><template><div> <Component3><template v-slot:default><h1>设置组件3的内容</h1><button>组件三的按钮</button></template>
</Component3></div>
</template><style scoped>
</style>

结果3:

 

②说明

在父组件中引入子组件时没有给插槽赋值时,备用内容会生效。

在子组件的slot中未设定插槽的名字,则默认名为default,在父组件中通过一下两种方式对插槽内容进行设置,如下:

        #插槽名:#default

        v-slot:插槽名:v-slot:default

所以上面中的结果2和结果3是一致的。

插槽中可以填充任何模板代码。

(2)具名插槽

具名插槽是指<slot> 元素设置一个特殊的属性name,用来给各个插槽分配唯一的 ID,用来给特定的插槽设置内容。

①示例

子组件:

<template>
<slot>组件3的默认内容</slot>
<slot name="h1">h1</slot>
<slot name="h2">h2</slot>
<slot name="h3">h3</slot>
</template>

父组件:

<!-- eslint-disable vue/require-v-for-key -->
<script setup>import Component3 from '@/components/Component3.vue';</script><template><div> <Component3><template v-slot:default><h1>设置组件3的内容</h1><button>组件三的按钮</button></template><template #h1><h1>设置组件3的内容h1</h1></template><template #h2><h1>设置组件3的内容h2</h1>  </template><template #h3><h1>设置组件3的内容h3</h1></template></Component3></div>
</template><style scoped>
</style>

结果:

 

(3)作用域插槽

作用域插槽可以让父级外层组件能够访问子组件的数据,子组件向将数据提供给插槽,组件 props 传递数据的方式,子组件向插槽传递一个attributes,父组件通过v-slot带的值(任意命名)来获取子组件的数据。

①示例:

子组件:

<template>
<slot name="h1">h1</slot>
<slot name="h2">h2</slot>
<slot name="h3">h3</slot>
<slot con="123" remark="234">作用域插槽</slot>
</template>

父组件:

 

<!-- eslint-disable vue/require-v-for-key -->
<script setup>import Component3 from '@/components/Component3.vue';</script><template><div> 
<Component3><template #h1><h1>设置组件3的内容h1</h1></template><template #h2><h1>设置组件3的内容h2</h1>  </template><template #h3><h1>设置组件3的内容h3</h1></template><template v-slot="res">{{ res.con }} -- {{ res.remark }}</template></Component3></div>
</template><style scoped>
</style>

结果:

 

②说明

 通过作用域插槽,父组件可以获取子组件的信息。常见的场景如在表格后面追加操作列,在操作列中添加按钮,在按钮操作中需要将当前行的数据传递到点击事件中,这是需要获取子组件中此行的数据,这是使用了作用域插槽,
有的框架中的作用域插槽使用的是默认作用域插槽,即插槽名称为default,有的使用的是具名作用域插槽。

(4)具名作用域插槽

具名作用域插槽就是给插槽设置name属性

①示例:

子组件:

<template>
<slot name="h1">h1</slot>
<slot name="h2">h2</slot>
<slot name="h3">h3</slot>
<slot con="123" remark="234">作用域插槽</slot>
<slot name="h4" con="123" remark="234" info="信息系">h4</slot>
</template>

父组件:

<!-- eslint-disable vue/require-v-for-key -->
<script setup>import Component3 from '@/components/Component3.vue';</script><template><div> 
<Component3><template #h1><h1>设置组件3的内容h1</h1></template><template #h2><h1>设置组件3的内容h2</h1>  </template><template #h3><h1>设置组件3的内容h3</h1></template><template v-slot="res"><h1>{{ res.con }} -- {{ res.remark }}</h1> </template><template #h4="{con,remark,info}"><h1>{{ con }} -- {{ remark }} -- {{ info }}</h1></template></Component3></div>
</template><style scoped>
</style>

结果:

说明:

父组件中作用域插槽的名字可以任意,也可以直接解构出来进行使用

(5)动态插槽

动态插槽是指插槽的名字可以设置变量。

①示例

子组件:

<template>
<slot name="h1">h1</slot>
<slot name="h2">h2</slot>
<slot name="h3">h3</slot>
<slot con="123" remark="234">作用域插槽</slot>
<slot name="h4" con="123" remark="234" info="信息系">h4</slot>
<slot name="h5">h5</slot>
</template>

父组件:

<!-- eslint-disable vue/require-v-for-key -->
<script setup>import Component3 from '@/components/Component3.vue';
const info5 = ref("h5")</script><template><div> 
<Component3><template #h1><h1>设置组件3的内容h1</h1></template><template #h2><h1>设置组件3的内容h2</h1>  </template><template #h3><h1>设置组件3的内容h3</h1></template><template v-slot="res"><h1>{{ res.con }} -- {{ res.remark }}</h1> </template><template #h4="{con,remark,info}"><h1>{{ con }} -- {{ remark }} -- {{ info }}</h1></template><template #[info5]><h1>设置组件3的内容h5</h1></template>
</Component3></div>
</template><style scoped>
</style>

结果:

3.总结

在前端开发中,插槽的使用很广泛,常见的如在表格中的使用,获取当前行的内容,当前行的下标,设置一些标题等等,使用前端模板框架时,可以根据说明文档,进行使用

参照:

插槽 Slots | Vue.js

vue3 | 插槽的使用_vue3具名插槽的用法-CSDN博客

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/473735.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

数学实验第三版(主编:李继成 赵小艳)课后练习答案(十三)(1)

实验十三&#xff1a;数据拟合与数据差值 练习一 1.采用不同的拟合函数拟合示例4中的数据点,然后利用弧长公式预测光缆长度,同正文中的结果进行比较. clc;clear; p[9.01,8.96,7.96,7.97,8.02,9.05,10.13,11.18,12.26,13.28,13.32,12.61,11.29,10.22,9.15,7.90,7.95,8.86,9.8…

SQL28 计算用户8月每天的练题数量(date_format函数的用法)

代码 select day(date) as day ,count(question_id) as question_cnt from question_practice_detail where date_format(date,%Y-%m)2021-08 group by day知识点 day函数取出日期格式数值里的日期,month,year函数也是类似的作用date_format规定日期/时间的输出格式 %Y 年&am…

PLC-Recorder的延伸分析功能说明

目录 一、缘起 二、如何从PLC-Recorder获取数据 1、在线获取 2、全自主打开数据文件 3、延伸分析 三、设置方法 四、效果展示 一、缘起 在各个行业&#xff0c;在不同的场景中&#xff0c;朋友们拿到数据后&#xff0c;想做的事情五花八门&#xff0c;有做宏观分析的、…

普中51单片机学习(六)

点亮第一个LED LED相关知识 LED,即发光二极管&#xff0c;是一种半导体固体发光器件。工作原理为&#xff1a;LED的工作是有方向性的&#xff0c;只有当正级接到LED阳极&#xff0c;负极接到LED的阴极的时候才能工作&#xff0c;如果反接LED是不能正常工作的。其原理图如下 …

001 - Hugo, 创建一个网站

001 - Hugo, 创建一个网站安装hugoWindows系统Macos Hugo博客搭建初始化博客主题安装配置博客各个页面开始创作创建 GitHub Page 仓库本地调试和预览发布内容 教程及鸣谢文字教程视频教程 001 - Hugo, 创建一个网站 这篇文章假设你已经&#xff1a; 了解基本的终端命令行知识&…

C++模板进阶操作 —— 非类型模板参数、模板的特化

C模板进阶 非类型模板参数模板的特化概念 函数模板特化类模板特化全特化 偏特化模板总结 非类型模板参数 模板参数可分为类型形参和非类型形参。 类型形参&#xff1a; 出现在模板参数列表中&#xff0c;跟在class或typename关键字之后的参数类型名称。 非类型形参&#xff1a…

C++初阶(十二) stack和queue

一、stack的介绍和使用 1.1 stack的介绍 stack的文档介绍 stack是一种容器适配器&#xff0c;专门用在具有后进先出操作的上下文环境中&#xff0c;其删除只能从容器的一端进行 元素的插入与提取操作。stack是作为容器适配器被实现的&#xff0c;容器适配器即是对特定类封装作…

Paper - CombFold: predicting structures of large protein assemblies 论文简读

欢迎关注我的CSDN&#xff1a;https://spike.blog.csdn.net/ 本文地址&#xff1a;https://spike.blog.csdn.net/article/details/136143199 CombFold: predicting structures of large protein assemblies using a combinatorial assembly algorithm and AlphaFold2 CombFold…

贪心算法之合并区间

“任世界多宽广&#xff0c;停泊在这港口~” 区间问题&#xff0c;涉及到最多的就是 取交集 和 并集的概念。我们使用C排序算法后&#xff0c;其默认规则就是按照 “左排序”进行的。因而&#xff0c;我们实质上注意的是每一个区间的 右端点&#xff0c;根据题目要求&#xff…

【Python如何在列表中随机抽出一个元素】

1、python代码如下&#xff1a; import random a [2, 4, 8, 9, "whats up"] q random.choice(a) # 随机从列表a中输出一个元素 b random.choices(a) # 随机从列表a中取出一个元素输出一个列表 lucky_num random.randint(1, 50) # 随机从1-50中取出一个整数包…

UnityShader——06UnityShader介绍

UnityShader介绍 UnityShader的基础ShaderLab UnityShader属性块介绍 Properties {//和public变量一样会显示在Unity的inspector面板上//_MainTex为变量名&#xff0c;在属性里的变量一般会加下划线&#xff0c;来区分参数变量和临时变量//Texture为变量命名//2D为类型&…

如何使用iptables或者firewalld配置Linux系统的防火墙策略

在网络安全中&#xff0c;防火墙是一种关键的安全设备&#xff0c;用于保护计算机网络免受恶意攻击和未经授权的访问。在Linux系统中&#xff0c;我们可以使用iptables或者firewalld来配置防火墙策略。本文将介绍如何使用这两种工具来配置Linux系统的防火墙策略&#xff0c;包括…