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

第IV章-Ⅱ Vue3中的插槽使用

第IV章-Ⅱ Vue3中的插槽使用

  • 基本插槽
    • 默认内容
  • 具名插槽
  • 作用域插槽

在 Vue 3 中,插槽(slots)是一种强大的模式,用于将模板代码从父组件注入到子组件中,使得子组件的内容可以在使用时被自定义。Vue 3 中的插槽用法包括基本插槽、具名插槽和作用域插槽。

基本插槽

基本插槽允许父组件向子组件传递内容,这些内容在子组件的模板中通过一个简单的 标签进行定义和显示。

  • 子组件
<template><div><slot></slot> <!-- 默认插槽 --></div>
</template><script lang="ts">
import { defineComponent } from 'vue';export default defineComponent({name: 'ChildComponent'
});
</script>
  • 父组件
<template><div><ChildComponent><p>This content will go into the default slot of the ChildComponent.</p></ChildComponent></div>
</template><script lang="ts">
import { defineComponent } from 'vue';
import ChildComponent from './ChildComponent.vue';export default defineComponent({name: 'ParentComponent',components: {ChildComponent}
});
</script>

默认内容

可以在 标签内部指定默认内容,如果父组件没有提供任何插槽内容,将显示默认内容。

  • 子组件
<template><div><slot>Default content if nothing is provided by the parent.</slot></div>
</template>

具名插槽

具名插槽允许你为不同的插槽内容定义多个插槽,每个插槽都有其唯一的名字。这样父组件可以针对特定的插槽提供内容。

  • 子组件
<template><div><slot name="header"></slot><slot name="main"></slot><slot name="footer"></slot></div>
</template><script lang="ts">
import { defineComponent } from 'vue';export default defineComponent({name: 'ChildComponent'
});
</script>
  • 父组件
<template><ChildComponent><template v-slot:header><h1>Header Content</h1></template><template v-slot:main><p>Main Content of the Page</p></template><template v-slot:footer><footer>Footer Details</footer></template></ChildComponent>
</template><script lang="ts">
import { defineComponent } from 'vue';
import ChildComponent from './ChildComponent.vue';export default defineComponent({name: 'ParentComponent',components: {ChildComponent}
});
</script>

作用域插槽

作用域插槽(Scoped Slots)是 Vue 中一种高级的插槽用法,允许子组件将其内部的数据传递回给使用这些插槽的父组件的插槽内容。这种方式不仅可以让父组件插入 HTML 或组件,还能让父组件访问子组件中定义的数据,非常适合创建高度可定制和复用的组件。

  • 子组件
<template><ul><!-- 使用作用域插槽将 todo 对象作为插槽的数据传递给父组件 --><li v-for="todo in todos" :key="todo.id"><slot name="todo" :todo-data="todo"><!-- 默认内容,如果父组件没有提供插槽模板 -->{{ todo.text }}</slot></li></ul>
</template><script lang="ts">
import { defineComponent, PropType } from 'vue';export default defineComponent({name: 'TodoList',props: {todos: Array as PropType<Array<{ id: number; text: string }>>}
});
</script>
  • 父组件
<template><TodoList :todos="todoItems"><!-- 定义如何使用 todo 数据渲染每个待办事项 --><template v-slot:todo="{ todo }"><strong>{{ todo.text }}</strong> (ID: {{ todo.id }})</template></TodoList>
</template><script lang="ts">
import { defineComponent, ref } from 'vue';
import TodoList from './components/TodoList.vue';export default defineComponent({name: 'App',components: {TodoList},setup() {const todoItems = ref([{ id: 1, text: 'Finish the report' },{ id: 2, text: 'Meet with the client' },{ id: 3, text: 'Prepare presentation' }]);return { todoItems };}
});
</script>

子组件提供了一个todo插槽,每一个todo的数据通过todo-data传递给插槽。父组件接收这个数据并自定义了如何显示每个项目。
提示:v-slot:todo 简写 #todo='todo'

http://www.lryc.cn/news/344326.html

相关文章:

  • 【半个月我拿下了软考证】软件设计师高频考点--系统化教学-网络安全
  • E2PROM读写函数
  • MySql中什么是回表? 如何减少回表的次数
  • 【Linux】目录和文件相关的命令,补充:centos7系统目录结构
  • 【读点论文】SAM-LIGHTENING: A LIGHTWEIGHT SEGMENT ANYTHING MODEL,改进自注意力机制,然后知识蒸馏提点
  • PostgreSQL函数和运算符
  • 使用网络工具监控网络性能
  • Gradle基础笔记
  • QT+网络调试助手+TCP客户端
  • 数据库调优-SQL语句优化
  • h函数 render函数 JSX基本用法
  • 购物车操作
  • 华为手机 鸿蒙系统-android studio识别调试设备,开启adb调试权限
  • 计算机网络——Dijkstra路由算法
  • AI智能化逐渐趋于成熟后,预测今后最吃香的开发职业
  • Acwing2024蓝桥杯BFS
  • vue打包报错:CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
  • 计算机组成原理网课笔记
  • Python学习第四部分 函数式编程
  • 数据结构-二叉树-AVL树(平衡二叉树)
  • 【Qt问题】windeployqt如何提取Qt依赖库
  • VS2019下使用MFC完成科技项目管理系统
  • 【Android】Kotlin学习之数据容器(数组for循环遍历)
  • JavaWeb_请求响应_简单参数实体参数
  • windows端口复用
  • [Redis] 使用布隆过滤器和分布式锁实现用户注册
  • Okhttp 发送https请求,忽略ssl认证
  • IT项目管理-大题【太原理工大学】
  • 【代码随想录】day48
  • 【补充】1-auth的使用、扩写auth的user表、django支持缓存