[Vue] Slots - 2. Scoped Slots

news/2024/12/2 1:56:09/文章来源:https://www.cnblogs.com/Answer1215/p/18580782

Introduction


Slots allow developers to have the flexibility to provide content to a child component, but what happens when the child component has control of the data? In these cases, scoped slots are here to come to the rescue.

What are scoped slots?


Scoped slots is a technique for allowing a component to expose data to the slot’s template block. While that may sound like a mouthful, if we take the analogy of a crane game, slots allow us to put data into it from the parent.

However, when we want to access data from the child, standard slots will not give us permission to grab that data. As a result, we need to expose the data from the child so that we can grab it and put it in the slot as desired.

In terms of code, let’s take a look at a Library.vue and Book.vue example:

📄Book.vue

<template><div class="book"><slot name="title"></slot></div>
</template><script>
export default {data() {return {bookTitle: 'Child Providing Data'}}
}
</script>

📄Library.vue

<template><Book><template v-slot:title><!-- How do we get the bookTitle from Book.vue? --></template></Book>
</template>

As we can see in the code example, the data is currently embedded in our Book.vue component and we need to some how render it in the Library.vue component. Let’s learn how we can use scoped slots!

 

How do you use scoped slots?


To learn how to use scoped slots, let’s start with a standard named slot with a name of header.

📄LogoHeader.vue

<template><slot name="header" />
</template><script>
export default {data() {return {logoImage: '/images/logo.png'}}
}
</script>

 

In this scenario, let’s say that we want to expose our logoImage property to the slot. To do this, we define a prop (like you would with a normal component) on our slot.

<template><slot name="header" :logo="logoImage" />
</template><script>
export default {data() {return {logoImage: '/images/logo.png'}}
}
</script>

By defining these “slot props,” this allows us to access them in the <template> block by exposing the slotProps value.

 

Once we expose the slot props, this allows us to use it within that <template> block.

Using our code from this scenario, it would result in the following:

📄LogoHeader.vue

<template><slot name="header" :logo="logoImage" />
</template><script>
export default {data() {return {logoImage: '/images/logo.png'}}
}
</script>

📄App.vue

<template><LogoHeader><template v-slot:header="slotProps">{{ slotProps.logo }}</template></LogoHeader>
</template>

 

 

Why it is called “scoped” slots

Let’s start by looking at our child component:

📄Book.vue

<template><div class="book"><slot name="title" :bookTitle="bookTitle" /></div>
</template><script>
export default {data() {return {bookTitle: 'Child Providing Data'}}
}
</script>

It provides the bookTitle data to the title slot, which we then access in our parent component:

📄Library.vue

<template><Book><template v-slot:title="slotProps"><h1>{{ slotProps.bookTitle }}</h1></template></Book>
</template>

With the bookTitle property exposed via slotProps, a common misconception is that it is now available to the parent component for use in things like methods, computed properties, or something else.

 

🛑 So in the event we wanted to create an uppercaseTitle in our Library.vue component, we might try to write code like this:

<script>
export default {computed: {uppercaseTitle() {// 🛑THIS DOES NOT WORKthis.slotProps.bookTitle.toUpperCase()}}
}
</script>

But this will not work, because the data that the child is exposing to the parent is only scoped to the slot template block.

While this may seem like an inconvenience, this ultimately helps developers by encouraging us to keep the concerns for each component in the correct place. In other words, rather than try to create a computed property in our Library.vue component, it would be more consistent to keep that logic inside of Book.vue instead and then expose it via slot props.

📄Book.vue

<template><div class="book"><slot name="title" :bookTitle="bookTitle" :uppercaseBookTitle="uppercaseTitle"/></div>
</template><script>
export default {data() {return {bookTitle: 'Child Providing Data'}},computed: {uppercaseTitle() {return this.bookTitle.toUpperCase()}}
}
</script>

So to reinforce the concept once more, any data exposed via slot props is scoped to the slot template and nothing more.

 

Destructuring slot props

When using slot props, it’s a common desire for developers to find ways to make their code more concise, in other words, less code. And because slotProps results in a JavaScript object, we can use ES6 destructuring to make our code a little easier to read.

📄Library.vue (before)

<template><Book><template v-slot:title="slotProps"><h1>{{ slotProps.bookTitle }}</h1></template></Book>
</template>

📄Library.vue (after)

<template><Book><template v-slot:title="{ bookTitle }"><h1>{{ bookTitle }}</h1></template></Book>
</template>

 

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

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

相关文章

【比赛游记】2024 ICPC 昆明站游记

希望能带来好运!2024.11.6 FZU 申请到了 2024 ICPC 昆明站的一个额外名额,并分给了我和另外两位同学。感谢傅老师!感谢实验室的各位伙伴们! Day -1 坐飞机飞往云南,云层之上的晚霞很美。 晚上九点多,和队友 vp 了一把 2024 北京市赛(这是我们队的第一次训练 ...),由于…

装机软件汇总

装机软件汇总 一些个人用的软件 编程相关JetBrainCrackLinkVSCodeGitRedis Insight:Redis官方可视化连接Navicat Premium 15:数据库可视化Wireshark:抓包Sandboxie-Plus:沙箱VMware:虚拟机SSH终端MobaXtermTermius效率UTools:Alt + Space = anythingSnipaste:截图天若OC…

基于Jenkins构建微服务项目发布失败回滚

基于 Jenkins 构建微服务项目发布失败回滚 1 微服务项目发布失败回滚1.1 安装插件 Active Choices(动态生成选项) 1.2 编写获取 harbor 仓库镜像列表的脚本 [root@k8s-master1 ms_jenkins]# cat get_tags.sh#!/bin/bash​# Harbor的用户名USER="admin"# Harbor的密码…

三维模型文件格式解读

最近接触到三维模型文件,发现格式五花八门的,这里做一个汇总和解读。最近接触到三维模型文件,发现格式五花八门的,这里做一个汇总和解读。 STL 简介 STL,全称为STereoLithography,译作:立体光刻,由3D System软件公司创立。但因三角网格形式的其特点,又可以翻译为“标准…

开源 - Ideal库 - Excel帮助类,TableHelper实现(三)

讲解对象集合与DataTable相互转换,包括表格转对象集合需校验类型、列名对应,及解决结构体赋值问题;对象集合转表格、一维数组转表格、行列转置的方法及示例代码,测试源码已上传。书接上回,我们今天继续讲解实现对象集合与DataTable的相互转换。01、把表格转换为对象集合 该…

信息安全概论复习3

保密与安全文档保密密级术语密码体制 单钥密码体制(对称密码体制)加密方式双钥密码体制(非对称密码体制)(公钥体制)特点

优化企业博客内容:策略与ai工具的应用

在数字化营销日益重要的今天,企业博客不仅是品牌传播的重要渠道,也是吸引潜在客户、建立品牌权威性的关键工具。然而,要让企业博客真正发挥作用,关键在于内容的优化。本文将探讨优化企业博客内容的策略,并详细介绍如何利用HelpLook平台来实施这些策略,以提升博客的吸引力…

攻防世界-Decrypt-the-Message

一、题目 收到一首英文诗歌和一段密文,要求很简单,就是解密这个密文二、解题 1、背景知识PoemCode参考文章:https://blog.csdn.net/xiao__1bai/article/details/1202504522、解密 了解加密原理即可,解密过程很复杂,可以直接用现成的脚本脚本地址:https://github.com/abpoly…

请问PbootCMS 登录失败及表单提交校验失败的解决方案

在使用 PbootCMS 时,可能会遇到登录失败、表单提交校验失败等问题。以下是针对这些常见问题的详细解决方案。 一、登录失败或表单提交校验失败 描述:用户尝试登录时失败。 表单提交时校验失败。解决方案:检查服务器环境:确保服务器环境满足 PbootCMS 的最低要求。 检查 PHP 版…

# 学期(2024-2025-1) 学号(20241420) 《计算机基础与程序设计》第10周学习总结

学期(2024-2025-1) 学号(20241420) 《计算机基础与程序设计》第10周学习总结 作业信息这个作业属于哪个课程 <班级的链接>(2024-2025-1-计算机基础与程序设计)这个作业要求在哪里 <作业要求的链接>(如2024-2025-1计算机基础与程序设计第一周作业)这个作业的目…

关于用户反馈到产品需求的思考

1 前言 在数字产品的世界中,用户反馈不仅是检验产品成功与否的试金石,更是推动产品迭代与创新的关键驱动力。作为项目负责人,我们深知每一个反馈背后蕴含的用户需求与期望。最近刚好看到了@玉伯大佬写的文章从用户反馈到产品需求的思考,深受启发,结合这一年半的工作经验,…

2024年11月文章一览

2024年11月编程人总共更新了21篇文章: 1.2024年10月文章一览 2.《使用Gin框架构建分布式应用》阅读笔记:p307-p392 3.《使用Gin框架构建分布式应用》阅读笔记:p393-p437 4.《使用Gin框架构建分布式应用》读后感 5.《Django 5 By Example》阅读笔记:p1-p16 6.《Django 5 By …