vue3 | slots
一、什么是插槽
插槽就是子组件中的提供给父组件使用的一个占位符,用<slot></slot> 表示,父组件可以在这个占位符中填充任何模板代码,如 HTML、组件等,填充的内容会替换子组件的<slot></slot>标签,父组件填充的内容称为插槽内容。
- 子组件不提供插槽时,父组件填充失效
- 父组件无填充时,
<slot></slot>中的备用内容会启用生效 - 父级模板里的所有内容都是在父级作用域中编译的,子模板里的所有内容都是在子作用域中编译的,互不影响
二、匿名插槽(默认插槽)
介绍
在外部组件没有提供任何内容的情况下,可以使用匿名插槽提供默认内容。
使用场景
比如
MyComponent.vue
<n-el>
<slot>这是一个默认展示的内容</slot>
</n-el>
//组件使用
<MyComponent/>
如下:
如果外部组件提供了插槽内容,我们提供的内容会覆盖掉默认的内容
如下:
<MyComponent>加入一段文字</MyComponent>
注: 插槽内容可以是任意合法的模板内容,不局限于文本。
如下:
<MyComponent>
<n-button>加入一个按钮</n-button>
</MyComponent>
三、具名插槽
介绍
<slot>元素带有 name 属性的插槽被称为具名插槽。
使用场景
作用于一个组件中拥有多个插槽,而name相当于插槽的标识,用来给各个插槽分配唯一的 ID。
MyComponent.vue
<n-el class="flex flex-col justify-center items-center h-[80vh] w-full">
<n-el class="text-[pink] text-[18px] mb-2">组件相关内容</n-el>
<slot name="head"></slot>
<slot name="main"></slot>
<slot name="footer"></slot>
</n-el>
外部引用该组件
<MyComponent>
<template v-slot:head>
<n-el>这是头部内容</n-el>
</template>
<template v-slot:main>
<n-el>这是主体内容</n-el>
</template>
<template v-slot:footer>
<n-el>这是尾部内容</n-el>
</template>
<template v-slot:no>
<n-el>组件中没有可匹配的插槽name,不显示</n-el>
</template>
</MyComponent>
v-slot可以简写成#
<MyComponent>
<template #head>
<n-el>这是头部内容</n-el>
</template>
<template #main>
<n-el>这是主体内容</n-el>
</template>
<template #footer>
<n-el>这是尾部内容</n-el>
</template>
<template #no>
<n-el>组件中没有可匹配的插槽name,不显示</n-el>
</template>
</MyComponent>
现在 <template> 元素中的所有内容都将被传递到相应的插槽。
注意: 匿名插槽也有自己的name,只不过 name 会被隐式地命名为default。
上面的写法等价于:
四、动态插槽名
<MyComponent>
<template v-slot:[slotName]>
...
</template>
<!-- 缩写为 -->
<template #[slotName]>
...
</template>
</MyComponent>
如下
MyComponent.vue
<n-el>
<n-el>组件相关内容</n-el>
<slot name="head"></slot>
</n-el>
<script setup lang="ts">
const data = 'head'
</script>
<template>
......
<MyComponent>
<template #[data]> 头部内容 </template>
</MyComponent>
.......
</template>
五、作用域插槽
介绍
作用域插槽可以让父级外层组件能够访问子组件的数据,子组件向将数据提供给插槽,组件 props 传递数据的方式,子组件向插槽传递一个attributes,父组件通过v-slot带的值(任意命名)来获取子组件的数据。
使用场景
(一)、默认插槽
MyComponent.vue
<n-el class="flex flex-col justify-center items-center h-[80vh] w-full">
<slot content="螺蛳粉" data="10"></slot>
</n-el>
写法一: v-slot 写在组件上
<MyComponent v-slot="res">
<n-el>老板:来 {{ res.data }} 份 {{ res.content }} ~</n-el>
</MyComponent>
注意: v-slot="res" 可以类比这里的函数签名,和函数的参数类似,我们也可以在 v-slot 中使用解构:
<MyComponent v-slot="{ data, content }">
<n-el>
老板:来 {{ data }} 份 {{ content }} ~
</n-el>
</MyComponent>
写法二: v-slot 写在 template 上
<MyComponent>
<template v-slot:default="res">
<n-el>老板:来 {{ res.data }} 份 {{ res.content }} ~</n-el>
</template>
//或者
<template #default="res">
<n-el>老板:来 {{ res.data }} 份 {{ res.content }} ~</n-el>
</template>
</MyComponent>
(二)、具名插槽
具名作用域插槽的工作方式也是类似的,插槽 props 可以作为 v-slot 指令的值被访问到:v-slot:name="slotProps"。当使用缩写时是这样:
MyComponent.vue
<n-el class="flex flex-col justify-center items-center h-[80vh] w-full">
<slot content="北京烤鸭" data="5" name="food1"></slot>
<slot content="长沙臭豆腐" data="15" name="food2"></slot>
</n-el>
<MyComponent>
<template #food1="res">
<n-el>老板:来 {{ res.data }} 份 {{ res.content }} ~</n-el>
</template>
<template #food2="res">
<n-el>老板:来 {{ res.data }} 份 {{ res.content }} ~</n-el>
</template>
</MyComponent>
vue3 | slots的更多相关文章
- 预计2019年发布的Vue3.0到底有什么不一样的地方?
摘要: Vue 3.0预览. 原文:预计今年发布的Vue3.0到底有什么不一样的地方? 作者:小肆 微信公众号:技术放肆聊 Fundebug经授权转载,版权归原作者所有. 还有几个月距离 vue2 的 ...
- 尝鲜 vue3.x 新特性 - CompositionAPI
0. 基础要求 了解常见的 ES6 新特性 ES6 的导入导出语法 解构赋值 箭头函数 etc... 了解 vue 2.x 的基本使用 组件 常用的指令 生命周期函数 computed.watch.r ...
- Vue3语法快速入门以及写一个倒计时组件
Vue3写一个倒计时组件 vue3 beta版本发布已有一段时间了,文档也大概看了一下,不过对于学一门技术,最好的方法还是实战,于是找了一个比较简单的组件用vue3来实现,参考的是vant的count ...
- vite + ts 快速搭建 vue3 项目 以及介绍相关特性
博客地址:https://ainyi.com/98 Vue3.0,One Piece 接下来得抽空好好学习了 vite 尤大在 Vue 3.0 beta 直播中推荐了 vite 的工具,强调:针对Vu ...
- vue3系列:vue3.0自定义弹框组件V3Popup|vue3.x手机端弹框组件
基于Vue3.0开发的轻量级手机端弹框组件V3Popup. 之前有分享一个vue2.x移动端弹框组件,今天给大家带来的是Vue3实现自定义弹框组件. V3Popup 基于vue3.x实现的移动端弹出框 ...
- vue3系列:vue3.0自定义全局弹层V3Layer|vue3.x pc桌面端弹窗组件
基于Vue3.0开发PC桌面端自定义对话框组件V3Layer. 前两天有分享一个vue3.0移动端弹出层组件,今天分享的是最新开发的vue3.0版pc端弹窗组件. V3Layer 一款使用vue3.0 ...
- vue3.0 composition API
一.Setup函数 1.创建时间:组件创建之前被调用,优先与created被调用,this指向的实例为window,created所指向的实例为proxy 2.this指向:不会指向组件实例 3.参数 ...
- Vue3.0新特性
Vue3.0新特性 Vue3.0的设计目标可以概括为体积更小.速度更快.加强TypeScript支持.加强API设计一致性.提高自身可维护性.开放更多底层功能. 描述 从Vue2到Vue3在一些比较重 ...
- 简单梳理下 Vue3 的新特性
在 Vue3 测试版刚刚发布的时候,我就学习了下 Composition API,但没想到正式版时隔一年多才出来,看了一下发现还是增加了不少新特性的,在这里我就将它们一一梳理一遍. 本文章只详细阐述 ...
- 基于vue3+electron11实现QQ登录切换|自定义导航栏|托盘|打包
上一篇有给大家分享过使用vue3和electron快速搭建项目.创建多窗口/父子modal窗口的一些方法.今天继续给大家分享一些vue3.x+electron11项目开发中的一些知识点/踩坑记录,希望 ...
随机推荐
- 成熟企业级开源监控解决方案Zabbix6.2关键功能实战-上
@ 目录 概述 定义 监控作用 使用理解 监控对象和指标 架构组成 常用监控软件分析 版本选型 俗语 安装 部署方式 部署 zabbix-agent 概述 定义 Zabbix 官网地址 https:/ ...
- wiki搭建详细过程及步骤
wiki搭建详细过程及步骤 1.查看yum库中jdk的版本 2.选择java-1.8.0安装 3.配置环境变量 4.环境变量生效 5.查看jdk是否安装成功 6.启动mysql服务 7.下载confl ...
- ubuntu 22.04安装多个gcc
sudo apt install gcc-9 g++-9 sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 90 - ...
- RabbitMq了解
RibbitMQ MQ优势 MQ的三大主要作用: 应用解耦.异步提速.流量削锋 应用解耦 系统的耦合性越高,容错性就越低,可维护性就越低: 解耦: 如果其中一个系统服务宕机,那么系统的其他服务将也无法 ...
- Qt_CLion
目录 安装Qt和CLion 配置 CLion配置Qt的资源文件系统 在项目根文件夹下创建一个资源文件夹 在项目根目录下创建一个qrc文件 安装Qt和CLion 相关的安装网上有很多教程,安装步骤这里不 ...
- Python数据类型+运算符
Python基础数据类型 上期练习讲解 # 练习一.想办法打印出jason l1 = [11, 22, 'kevin', ['tony', 'jerry', [123, 456, 'jason'] ] ...
- Java单例模式的最佳实践?
"读过书,--我便考你一考.茴香豆的茴字,怎样写的?"--鲁迅<孔乙己> 0x00 大纲 目录 0x00 大纲 0x01 前言 0x02 单例的正确性 new关键字 c ...
- socet网络编程
一. 网络模型 应用层 ----- HTTP协议 传输层 ----- TCP协议 网络层 ----- IP协议 链路层 ----- 以太网协议 二. ...
- PyTorch复现LeNet-5手写识别学习笔记
用PyTorch搭建LeNet-5手写识别 首先申明,这篇博客用于记录本人看完LeNet-5论文,并对其中的算法进行复现的记录,可以看成是学习笔记 这里只介绍复现的工作,如果想了解更多有关网络的细节, ...
- jenkins+git+.net core实现自动发布
一.前言 继上篇介绍jenkins过去2年多了,最近整理了一下,希望这篇能介绍到一些更实用的方法和知识. 本次使用的jenkins版本是2.375.1.jdk 17.WinRAR.git:发布时,可以 ...