Vue3 插槽系统详解

news/2025/2/5 14:03:50/

Vue3 插槽系统详解

1. 默认插槽

重点掌握:

  • 插槽的基本概念
  • 默认插槽的使用方法
  • 后备内容的设置

示例代码:

vue"><!-- BaseCard.vue -->
<template><div class="card"><div class="card-header"><!-- 后备内容 --><slot>默认内容</slot></div></div>
</template><!-- 使用组件 -->
<template><div><!-- 使用默认插槽 --><BaseCard><h2>这是插入到插槽的内容</h2><p>可以插入任意内容</p></BaseCard><!-- 不提供内容时使用后备内容 --><BaseCard /></div>
</template>

2. 具名插槽

重点掌握:

  • 多个插槽的命名
  • v-slot 指令的使用
  • 插槽缩写语法 #

示例代码:

vue"><!-- LayoutComponent.vue -->
<template><div class="layout"><header><slot name="header"></slot></header><main><slot></slot>  <!-- 默认插槽 --></main><footer><slot name="footer"></slot></footer></div>
</template><!-- 使用组件 -->
<template><LayoutComponent><!-- 具名插槽使用 --><template v-slot:header><h1>网站标题</h1></template><!-- 默认插槽内容 --><template v-slot:default><p>主要内容区域</p></template><!-- 缩写语法 --><template #footer><p>版权信息 © 2024</p></template></LayoutComponent>
</template>

3. 作用域插槽

重点掌握:

  • 插槽数据传递
  • 解构插槽 props
  • 多个作用域插槽的使用

示例代码:

vue"><!-- DataList.vue -->
<template><div><ul><li v-for="item in items" :key="item.id"><slot name="item" :item="item":index="index":isSelected="selectedId === item.id"><!-- 默认渲染 -->{{ item.name }}</slot></li></ul></div>
</template><script setup>
const props = defineProps({items: {type: Array,required: true}
})const selectedId = ref(null)
</script><!-- 使用组件 -->
<template><DataList :items="users"><!-- 基本用法 --><template v-slot:item="slotProps"><div class="user-item">{{ slotProps.item.name }} ({{ slotProps.index }})</div></template><!-- 使用解构 --><template #item="{ item, index, isSelected }"><div class="user-item" :class="{ 'selected': isSelected }"><h3>{{ item.name }}</h3><p>Position: {{ index + 1 }}</p></div></template></DataList>
</template><!-- 高级用法:表格组件 -->
<template><Table :data="users"><!-- 定义列的渲染方式 --><template #column="{ row }"><td>{{ row.name }}</td></template><!-- 自定义操作列 --><template #actions="{ row }"><button @click="editUser(row)">编辑</button><button @click="deleteUser(row)">删除</button></template></Table>
</template><!-- 实际应用示例:可复用的表单项组件 -->
<!-- FormField.vue -->
<template><div class="form-field"><label :for="id">{{ label }}</label><slot :id="id":value="modelValue":error="error":updateValue="updateValue"></slot><span class="error">{{ error }}</span></div>
</template><!-- 使用表单项组件 -->
<template><form @submit.prevent="onSubmit"><!-- 文本输入 --><FormFieldlabel="用户名"v-model="form.username"><template #default="{ id, value, updateValue, error }"><input:id="id"type="text":value="value"@input="updateValue($event.target.value)":class="{ 'has-error': error }"/></template></FormField><!-- 下拉选择 --><FormFieldlabel="角色"v-model="form.role"><template #default="{ id, value, updateValue }"><select:id="id":value="value"@change="updateValue($event.target.value)"><option value="admin">管理员</option><option value="user">普通用户</option></select></template></FormField></form>
</template>

4. 插槽最佳实践

重点掌握:

  1. 合理使用默认内容
  2. 插槽 props 的类型定义
  3. 动态插槽名的使用
  4. 性能注意事项

TypeScript 类型定义:

// 定义插槽 props 的类型
type ItemSlotProps = {item: {id: numbername: string[key: string]: any}index: numberisSelected: boolean
}// 组件定义
defineComponent({name: 'DataList',props: {items: {type: Array as PropType<ItemSlotProps['item'][]>,required: true}}
})// 动态插槽名使用
<template><div><slot :name="`column-${columnName}`" v-bind="columnData"></div>
</template>

性能优化:

vue"><!-- 避免不必要的插槽更新 -->
<template><div><slot name="item":item="item"<!-- 使用 memo 避免不必要的更新 -->:memoData="memo(() => expensiveComputation(item))"></slot></div>
</template><script setup>
import { memo } from 'vue'// 缓存计算结果
const memoizedData = memo(() => {return expensiveComputation()
})
</script>

这部分内容详细介绍了Vue3中的三种插槽类型:

  1. 默认插槽:最基本的内容分发机制
  2. 具名插槽:通过名字指定内容放置位置
  3. 作用域插槽:允许子组件向父组件传递数据

http://www.ppmy.cn/news/1569521.html

相关文章

visual studio安装

一、下载Visual Studio 访问Visual Studio官方网站。下载 Visual Studio Tools - 免费安装 Windows、Mac、Linux 在主页上找到并点击“下载 Visual Studio”按钮。 选择适合需求的版本&#xff0c;例如“Visual Studio Community”&#xff08;免费版本&#xff09;&#x…

计算机毕业设计Python+CNN卷积神经网络考研院校推荐系统 考研分数线预测 考研推荐系统 考研爬虫 考研大数据 Hadoop 大数据毕设 机器学习

温馨提示&#xff1a;文末有 CSDN 平台官方提供的学长联系方式的名片&#xff01; 温馨提示&#xff1a;文末有 CSDN 平台官方提供的学长联系方式的名片&#xff01; 温馨提示&#xff1a;文末有 CSDN 平台官方提供的学长联系方式的名片&#xff01; 作者简介&#xff1a;Java领…

std::reverse 和 ranges::reverse、std::sort 和 ranges::sort

1. std::reverse 和 ranges::reverse 的区别 std::reverse 定义&#xff1a;定义在 <algorithm> 头文件中。 参数&#xff1a;接受两个迭代器&#xff08;begin 和 end&#xff09;&#xff0c;表示要反转的范围。 用法&#xff1a; std::reverse(iterator begin, ite…

java使用pcap4j进行报文发送和接收

1、pcap4j框架使用的驱动Npcap需要进行安装 2、获取自己window电脑的mac地址设备 在cmd中使用的命令是getmac 使用程序获取服务器本身的mac地址 import org.pcap4j.core.PcapNativeException; import org.pcap4j.core.PcapNetworkInterface; import org.pcap4j.core.Pcaps;…

STM32单片机学习记录(2.2)

一、STM32 13.1 - PWR简介 1. PWR&#xff08;Power Control&#xff09;电源控制 &#xff08;1&#xff09;PWR负责管理STM32内部的电源供电部分&#xff0c;可以实现可编程电压监测器和低功耗模式的功能&#xff1b; &#xff08;2&#xff09;可编程电压监测器&#xff08;…

在排序数组中查找元素的第一个和最后一个位置(力扣)

一.题目介绍 二.题目解析 使用二分进行查找 2.1处理边界情况 如果数组为空&#xff0c;直接返回 [-1, -1]&#xff0c;因为无法找到目标值。 int[] ret new int[2]; ret[0] ret[1] -1; if (nums.length 0) return ret; 2.2查找左端点&#xff08;目标值开始位置&#…

240. 搜索二维矩阵||

参考题解&#xff1a;https://leetcode.cn/problems/search-a-2d-matrix-ii/solutions/2361487/240-sou-suo-er-wei-ju-zhen-iitan-xin-qin-7mtf 将矩阵旋转45度&#xff0c;可以看作一个二叉搜索树。 假设以左下角元素为根结点&#xff0c; 当target比root大的时候&#xff…

用BGP的路由聚合功能聚合大陆路由,效果显著不?

正文共&#xff1a;666 字 11 图&#xff0c;预估阅读时间&#xff1a;1 分钟 之前我们统计过中国境内的IP地址和路由信息&#xff08;你知道中国大陆一共有多少IPv4地址吗&#xff1f;&#xff09;&#xff0c;不过数量比较多&#xff0c;有8000多条。截止到2021年底&#xff…