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

Vue基础19之插槽

Vue基础19

  • 插槽
    • 不使用插槽
      • App.vue
      • Category.vue
    • 默认插槽:slot
      • App.vue
      • Category.vue
    • 具名插槽
      • App.vue
      • Category.vue
    • 作用域插槽
      • App.vue
      • Category.vue
    • 总结

插槽

不使用插槽

App.vue

<template><div class="bg"><Category :listData="food"  title="美食"/><Category :listData="games" title="游戏"/><Category :listData="movies" title="电影"/></div>
</template><script>
import Category from "@/components/Category";
export default {name: "App",components: {Category},data(){return{food:["火锅","烧烤","小龙虾","牛排"],games:["红色警戒","穿越火线","劲舞团","超级玛丽"],movies:["《教父》","《拆弹专家》","《你好,李焕英》","《无间道》"]}},methods:{}
}
</script><style lang="less">
.bg{display: flex;justify-content: space-around;margin-top: 100px;
}
</style>

Category.vue

<template><div class="bg"><h1>{{title}}</h1><ul><li v-for="(item,index) in listData" :key="index">{{item}}</li></ul></div>
</template><script>export default {name: "Category",props:["listData","title"]}
</script><style scoped lang="less">
.bg{height: 500px;width: 300px;background-color: skyblue;display: inline-block;h1{background-color: orange;text-align: center;}
}
</style>

在这里插入图片描述

默认插槽:slot

<Category title="美食"><img src="https://s3.ax1x.com/2021/01/16/srJlq0.jpg" alt="">
</Category>
    <!-- 定义一个插槽(挖个坑,等着组件的使用者进行填充)   --><slot>我是一些默认值,当使用者没有传递具体结构时,我会出现</slot>

图片链接:
https://s3.ax1x.com/2021/01/16/srJlq0.jpg
视频链接:
http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4

App.vue

<template><div class="bg"><Category title="美食"><img src="https://s3.ax1x.com/2021/01/16/srJlq0.jpg" alt=""></Category><Category title="游戏"><ul><li v-for="(g,index) in games" :key="index">{{g}}</li></ul></Category><Category title="电影"><video controls src="http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"></video></Category><Category title="测试默认情况"></Category></div>
</template><script>
import Category from "@/components/Category";
export default {name: "App",components: {Category},data(){return{food:["火锅","烧烤","小龙虾","牛排"],games:["红色警戒","穿越火线","劲舞团","超级玛丽"],movies:["《教父》","《拆弹专家》","《你好,李焕英》","《无间道》"]}},methods:{}
}
</script><style lang="less">
.bg{display: flex;justify-content: space-around;margin-top: 100px;img{width: 100%;}video{width: 100%;}
}
</style>

Category.vue

<template><div class="bg"><h1>{{title}}</h1><!-- 定义一个插槽(挖个坑,等着组件的使用者进行填充)   --><slot>我是一些默认值,当使用者没有传递具体结构时,我会出现</slot></div>
</template><script>export default {name: "Category",props:["title"]}
</script><style scoped lang="less">
.bg{height: 500px;width: 300px;background-color: skyblue;display: inline-block;h1{background-color: orange;text-align: center;}
}
</style>

在这里插入图片描述

具名插槽

<Category title="美食"><img slot="center" src="https://s3.ax1x.com/2021/01/16/srJlq0.jpg" alt=""><a slot="footer"  href="http://www.baidu.com" class="more">更多美食</a>
</Category>
<!-- 定义多个插槽(挖个坑,等着组件的使用者进行填充)   --><slot name="center">我是一些默认值,当使用者没有传递具体结构时,我会出现</slot><slot name="footer">我是一些默认值,当使用者没有传递具体结构时,我会出现</slot>

App.vue

<template><div class="bg"><Category title="美食"><img slot="center" src="https://s3.ax1x.com/2021/01/16/srJlq0.jpg" alt=""><a slot="footer"  href="http://www.baidu.com" class="more">更多美食</a></Category><Category title="游戏"><ul slot="center"><li v-for="(g,index) in games" :key="index">{{g}}</li></ul><div slot="footer" class="foot"><a href="http://www.baidu.com/">单机游戏</a><a href="http://www.baidu.com/">网络游戏</a></div></Category><Category title="电影"><video slot="center" controls src="http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"></video><!--  只有template标签可以使用v-slot:xxx 的形式   --><template v-slot:footer><div class="foot"><a href="http://www.baidu.com">经典</a><a href="http://www.baidu.com">热门</a><a href="http://www.baidu.com">推荐</a></div><h4 class="welcome">欢迎前来观影!</h4></template></Category></div>
</template><script>
import Category from "@/components/Category";
export default {name: "App",components: {Category},data(){return{food:["火锅","烧烤","小龙虾","牛排"],games:["红色警戒","穿越火线","劲舞团","超级玛丽"],movies:["《教父》","《拆弹专家》","《你好,李焕英》","《无间道》"]}},methods:{}
}
</script><style lang="less">
.bg{display: flex;justify-content: space-around;margin-top: 100px;img{width: 100%;}video{width: 100%;}.more{display: block;text-align: center;margin-top: 10px;}.foot{display: flex;justify-content: space-around;}.welcome{text-align: center;}
}
</style>

Category.vue

<template><div class="bg"><h1>{{title}}</h1><!-- 定义多个插槽(挖个坑,等着组件的使用者进行填充)   --><slot name="center">我是一些默认值,当使用者没有传递具体结构时,我会出现</slot><slot name="footer">我是一些默认值,当使用者没有传递具体结构时,我会出现</slot></div>
</template><script>export default {name: "Category",props:["title"]}
</script><style scoped lang="less">
.bg{height: 500px;width: 300px;background-color: skyblue;display: inline-block;h1{background-color: orange;text-align: center;}
}
</style>

在这里插入图片描述

作用域插槽

给使用者传值

<!-- 定义一个插槽(挖个坑,等着组件的使用者进行填充)   --><slot :games="games">我是一些默认值,当使用者没有传递具体结构时,我会出现</slot>

使用者通过template中的scope获取值

<template scope="obj"><ul><li v-for="(g,index) in obj.games" :key="index">{{g}}</li></ul>
</template>

App.vue

<template><div class="bg"><Category title="游戏"><template scope="obj"><ul><li v-for="(g,index) in obj.games" :key="index">{{g}}</li></ul></template></Category><Category title="游戏"><template scope="{games}"><ol><li style="color: red;" v-for="(g,index) in games" :key="index">{{g}}</li></ol></template></Category><Category title="游戏"><template slot-scope="{games}"><h4   v-for="(g,index) in games" :key="index">{{g}}</h4></template></Category></div>
</template><script>
import Category from "@/components/Category";
export default {name: "App",components: {Category},
}
</script><style lang="less">
.bg{display: flex;justify-content: space-around;margin-top: 100px;img{width: 100%;}video{width: 100%;}.more{display: block;text-align: center;margin-top: 10px;}.foot{display: flex;justify-content: space-around;}.welcome{text-align: center;}h4{text-align: center;}
}
</style>

Category.vue

<template><div class="bg"><h1>{{title}}</h1><!-- 定义一个插槽(挖个坑,等着组件的使用者进行填充)   --><slot :games="games">我是一些默认值,当使用者没有传递具体结构时,我会出现</slot></div>
</template><script>export default {name: "Category",props:["title"],data(){return{games:["红色警戒","穿越火线","劲舞团","超级玛丽"]}}}
</script><style scoped lang="less">
.bg{height: 500px;width: 300px;background-color: skyblue;display: inline-block;h1{background-color: orange;text-align: center;}
}
</style>

在这里插入图片描述

总结

  1. 作用:让父组件可以向子组件指定位置插入html结构,也是一种组件间通信的方式,适用于父组件===>子组件
  2. 分类:默认插槽、具名插槽、作用域插槽
  3. 使用方式:
    (1)默认插槽:
    父组件中:
<Category><div>html结构1</div>
</Category>

子组件中:

<template><div><!-- 定义插槽 --><slot>插槽默认内容</slot></div>
</template>

(2)具名插槽:
父组件中:

<Category><template slot="center"><div>html结构1</div></template><template v-slot:footer><div>html结构2</div></template>
</Category>

子组件中:

<template><div><!-- 定义插槽--><slot name="center">插槽默认内容...</slot><slot name="footer">插槽默认内容...</slot></div>
</template>

(3)作用域插槽
①理解:数据在组件的自身,但根据数据生成的结构需要组件的使用者来决定。(games数据在Category组件中,但使用数据所遍历出来的结构由App组件决定)
②具体编码:
父组件中:

<Category><template scope="scopeData"><!--生成的是ul列表--><ul><li v-for="g in scopeData.games" :key="g">{{g}}</li></ul></template>
</Category><Category><template slot-scope="scopeData"><!--生成的是h4标题--><h4 v-for="g in scopeData.games" :key="g">{{g}}</h4></template>
</Category>

子组件中:

<template><div><slot :games="games"></slot></div>
</template><script>export default {name: "Category",props:["title"],data(){return{games:["红色警戒","穿越火线","劲舞团","超级玛丽"]}}}
</script>

相关文章:

Vue基础19之插槽

Vue基础19插槽不使用插槽App.vueCategory.vue默认插槽&#xff1a;slotApp.vueCategory.vue具名插槽App.vueCategory.vue作用域插槽App.vueCategory.vue总结插槽 不使用插槽 App.vue <template><div class"bg"><Category :listData"food"…...

[Gin]框架底层实现理解(一)

前言&#xff1a;路由原理———压缩字典 这边简单讲一下gin非常重要的一个基点&#xff0c;也就是他作为go web框架的一个亮点 也就是Trie树和压缩字典算法 gin 通过树来存储路由&#xff0c;讲路由的字符拆解为一个个的结点&#xff0c;在获取handler函数时&#xff0c;会…...

css3横向无限公告消息滚动功能

html部分 {{item}}css部分 .boxingeds{ display: flex; flex-wrap: wrap; width: 150%; position: relative; left: 1000rpx; padding: 30rpx 0; position: absolute; top: 23%; z-index: 2; -webkit-animation: myfirst 30s linear 2s infinite; .textname{ display: inlin…...

【Git】Git工作流程及使用

Git工作流程及使用Git工作流程与常用命令Git工作流程Git常用命令项目中使用Git的场景需求开发前的分支拉取流程&#xff0c;需求开发后的分支合并流程分支合并出现冲突如何解决线上出现事故代码如何回退Git工作流程与常用命令 Git工作流程 workspace&#xff1a;工作区 stagin…...

降本增效,合作伙伴营销助力业绩增长

事实上&#xff0c;SaaS品牌透过“推荐奖励计划” 带来的业务营收平均占比高达 30%。例如&#xff0c;Evernote超过11300万用户通过老用户推荐来到Everote&#xff1b;Trello每日注册用户中有35%来自用户推荐&#xff1b;PayPal自从推行“推荐奖励计划”以来&#xff0c;用户日…...

【独家】华为OD机试 - 运动会(C 语言解题)

最近更新的博客 华为od 2023 | 什么是华为od,od 薪资待遇,od机试题清单华为OD机试真题大全,用 Python 解华为机试题 | 机试宝典【华为OD机试】全流程解析+经验分享,题型分享,防作弊指南)华为od机试,独家整理 已参加机试人员的实战技巧文章目录 最近更新的博客使用说明本期…...

【每天学习一点新知识】JNDI注入

什么是JNDIJNDI是Java的一种API&#xff0c;为我们提供了查找和访问各种命名和目录服务的通用统一的接口。通过JNDI统一接口我们可以来访问各种不同类型的服务&#xff0c;例如远程方法调用&#xff08;RMI&#xff09;&#xff0c;通用对象请求代理体系结构&#xff08;CORBA&…...

Transwarp KunDB 实施方案

星环科技 KunDB 实施方案方案描述优点缺点定期全量逻辑备份基于kundb导入导出工具&#xff0c;定期向kundb导出全量的逻辑数据,恢复时向kundb导入最近全量的逻辑数据。如每天00&#xff1a;00进行一次全量逻辑备份。1. 数据可视化2.方便问题排查3.还原失败不影响数据库的运行状…...

Redis学习之主从复制(八)

这里写目录标题一、主从复制简介1.1原理1.2 主从复制的作用二、主从复制工作流程2.1 建立连接2.1.1 master和slave连接流程2.1.2 master和slave互联2.1.3主从断开连接&#xff08;了解&#xff09;2.1.4 授权访问&#xff08;了解&#xff09;2.2 数据同步2.3 命令传播2.3.1 命…...

mysql8.0安装

创建文件 mkdir /usr/local/mysql mkdir /usr/local/mysql/data cd /usr/local/mysql 下载 wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz 解压 xz -d mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz tar xvf mysql-8.0.20-…...

前端经典面试题(有答案)

代码输出结果var a 10var obj {a: 20,say: () > {console.log(this.a)}}obj.say() var anotherObj { a: 30 } obj.say.apply(anotherObj) 输出结果&#xff1a;10 10我么知道&#xff0c;箭头函数时不绑定this的&#xff0c;它的this来自原其父级所处的上下文&#xff0c…...

华为云服务器安装mysql连接失败问题

新买了一个华为云服务器&#xff0c;装了一个宝塔linux工具&#xff0c;很好用&#xff0c;很好用。安装软件&#xff0c;管理软件都很方便。具体怎么操作官方文档很详细&#xff0c;不在这里赘述了。 问题&#xff1a;安装好mysql&#xff0c;安全组开放3306端口。修改root连接…...

合作伙伴管理软件VS CRM,企业应该选择哪一个?

当涉及到管理你公司的伙伴关系和与客户的关系时&#xff0c;有两个主要选择&#xff1a;合作伙伴管理软件和CRM&#xff08;客户关系管理&#xff09;软件。虽然这两种工具都可以帮助你跟踪商业关系的重要信息&#xff0c;但它们都有各自的优势和不足。 合作伙伴管理软件是专门…...

Matter 系列 #9|乐鑫 Matter 预配置服务加速设备生产

乐鑫 Matter 系列文章 #9 目录 Matter 预配置服务 1. 设备认证 (Device Attestation) 2. 独特性 (Uniqueness) 3. 安全性 (Security) 联系我们​​​​​​​ 如今&#xff0c;物联网行业蓬勃发展&#xff0c;大量市场参与者正在积极地构建 Matter 智能设备。 乐鑫一直致…...

手把手交叉编译mysql

1.下载mysql&#xff08;注意下载boost版本&#xff0c;这样会少一步编译&#xff09; 下载mysql的时候一定要看好交叉编译工具链的版本。因为mysql 8.0需要的工具链版本较高&#xff0c;所以有可能不支持 查看链接如下&#xff1a; MySQL :: MySQL 8.0 Reference Manual :: …...

升压模块直流隔离低压转高压稳压电源5v12v24v转50V100V110V150V200V250V400V500V600V800V1000V

特点效率高达80%以上1*2英寸标准封装单电压输出价格低稳压输出工作温度: -40℃~85℃阻燃封装&#xff0c;满足UL94-V0 要求温度特性好可直接焊在PCB 上应用HRB W2~40W 系列模块电源是一种DC-DC升压变换器。该模块电源的输入电压分为&#xff1a;4.5~9V、9~18V、及18~36VDC标准&…...

LeetCode:977 有序数组平方

给你一个按 非递减顺序 排序的整数数组 nums&#xff0c;返回 每个数字的平方 组成的新数组&#xff0c;要求也按 非递减顺序 排序。 示例 1&#xff1a; 输入&#xff1a;nums [-4,-1,0,3,10] 输出&#xff1a;[0,1,9,16,100] 解释&#xff1a;平方后&#xff0c;数组变为 […...

JAVA环境配置多个环境(全,详细,简单)

下载java包&#xff1a;https://www.oracle.com/java/technologies/downloads &#xff08;8版本稳定&#xff09; 直接无脑安装java程序 &#xff08;包括jdk-开发与jre-运行&#xff09; 接下来是java环境配置&#xff1a; 创建系统变量 &#xff08;用户变量也可以&#…...

10 Seata配置Nacos注册中心和配置中心

Seata配置Nacos注册中心和配置中心 Seata支持注册服务到Nacos&#xff0c;以及支持Seata所有配置放到Nacos配置中心&#xff0c;在Nacos中统一维护&#xff1b; 高可用(集群)模式下就需要配合Nacos来完成: 具体配置如下 注册中心 Seata-server端配置注册中心&#xff0c;…...

[数据库]表的增删改查进阶

●&#x1f9d1;个人主页:你帅你先说. ●&#x1f4c3;欢迎点赞&#x1f44d;关注&#x1f4a1;收藏&#x1f496; ●&#x1f4d6;既选择了远方&#xff0c;便只顾风雨兼程。 ●&#x1f91f;欢迎大家有问题随时私信我&#xff01; ●&#x1f9d0;版权&#xff1a;本文由[你帅…...

谷歌浏览器插件

项目中有时候会用到插件 sync-cookie-extension1.0.0&#xff1a;开发环境同步测试 cookie 至 localhost&#xff0c;便于本地请求服务携带 cookie 参考地址&#xff1a;https://juejin.cn/post/7139354571712757767 里面有源码下载下来&#xff0c;加在到扩展即可使用FeHelp…...

YSYX学习记录(八)

C语言&#xff0c;练习0&#xff1a; 先创建一个文件夹&#xff0c;我用的是物理机&#xff1a; 安装build-essential 练习1&#xff1a; 我注释掉了 #include <stdio.h> 出现下面错误 在你的文本编辑器中打开ex1文件&#xff0c;随机修改或删除一部分&#xff0c;之后…...

C# 类和继承(抽象类)

抽象类 抽象类是指设计为被继承的类。抽象类只能被用作其他类的基类。 不能创建抽象类的实例。抽象类使用abstract修饰符声明。 抽象类可以包含抽象成员或普通的非抽象成员。抽象类的成员可以是抽象成员和普通带 实现的成员的任意组合。抽象类自己可以派生自另一个抽象类。例…...

鸿蒙中用HarmonyOS SDK应用服务 HarmonyOS5开发一个生活电费的缴纳和查询小程序

一、项目初始化与配置 1. 创建项目 ohpm init harmony/utility-payment-app 2. 配置权限 // module.json5 {"requestPermissions": [{"name": "ohos.permission.INTERNET"},{"name": "ohos.permission.GET_NETWORK_INFO"…...

MySQL用户和授权

开放MySQL白名单 可以通过iptables-save命令确认对应客户端ip是否可以访问MySQL服务&#xff1a; test: # iptables-save | grep 3306 -A mp_srv_whitelist -s 172.16.14.102/32 -p tcp -m tcp --dport 3306 -j ACCEPT -A mp_srv_whitelist -s 172.16.4.16/32 -p tcp -m tcp -…...

C++_哈希表

本篇文章是对C学习的哈希表部分的学习分享 相信一定会对你有所帮助~ 那咱们废话不多说&#xff0c;直接开始吧&#xff01; 一、基础概念 1. 哈希核心思想&#xff1a; 哈希函数的作用&#xff1a;通过此函数建立一个Key与存储位置之间的映射关系。理想目标&#xff1a;实现…...

02.运算符

目录 什么是运算符 算术运算符 1.基本四则运算符 2.增量运算符 3.自增/自减运算符 关系运算符 逻辑运算符 &&&#xff1a;逻辑与 ||&#xff1a;逻辑或 &#xff01;&#xff1a;逻辑非 短路求值 位运算符 按位与&&#xff1a; 按位或 | 按位取反~ …...

Python 高级应用10:在python 大型项目中 FastAPI 和 Django 的相互配合

无论是python&#xff0c;或者java 的大型项目中&#xff0c;都会涉及到 自身平台微服务之间的相互调用&#xff0c;以及和第三发平台的 接口对接&#xff0c;那在python 中是怎么实现的呢&#xff1f; 在 Python Web 开发中&#xff0c;FastAPI 和 Django 是两个重要但定位不…...

深入浅出JavaScript中的ArrayBuffer:二进制数据的“瑞士军刀”

深入浅出JavaScript中的ArrayBuffer&#xff1a;二进制数据的“瑞士军刀” 在JavaScript中&#xff0c;我们经常需要处理文本、数组、对象等数据类型。但当我们需要处理文件上传、图像处理、网络通信等场景时&#xff0c;单纯依赖字符串或数组就显得力不从心了。这时&#xff…...

LeetCode 0386.字典序排数:细心总结条件

【LetMeFly】386.字典序排数&#xff1a;细心总结条件 力扣题目链接&#xff1a;https://leetcode.cn/problems/lexicographical-numbers/ 给你一个整数 n &#xff0c;按字典序返回范围 [1, n] 内所有整数。 你必须设计一个时间复杂度为 O(n) 且使用 O(1) 额外空间的算法。…...