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

做网站工作条件公司员工培训方案

做网站工作条件,公司员工培训方案,广东省建设银行招聘网站,武义企业网站建设第IV章-Ⅱ Vue3中的插槽使用 基本插槽默认内容 具名插槽作用域插槽 在 Vue 3 中,插槽(slots)是一种强大的模式,用于将模板代码从父组件注入到子组件中,使得子组件的内容可以在使用时被自定义。Vue 3 中的插槽用法包括基…

第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.khdw.cn/news/1150.html

相关文章:

  • 大连seo建站公司爱站网关键词
  • 如何查看网站cms系统百度一下京东
  • 基层政府建设管理网站网络营销的主要方式
  • 河北辛集住房和城乡建设厅网站web设计一个简单网页
  • 服务器做网站哪个系统好seo每日一帖
  • 广西壮族自治区住房和建设厅网站抖音推广方式有哪些
  • 佛山做网站建设大连百度网站排名优化
  • 网站栏目怎么做单独的搜索框欧洲网站服务器
  • 杭州做外贸网站网站上做推广
  • 网站建设 联系我们人员优化方案怎么写
  • 做高仿批发的网站有哪些网站后台管理系统
  • 西宁网站建设开发公司国际购物网站平台有哪些
  • 专门做书籍设计的网站百家号官网
  • 福州网站建设索q479185700百度关键词推广教程
  • 衡水网站建设十种营销方式
  • 旅行社网站开发郑州seo排名扣费
  • 网站做页游推广陕西网络营销优化公司
  • 莱西做网站广告推广软件
  • 咸阳网站建设报价长沙关键词优化新行情报价
  • 厦门建设管理局网站首页网站排名快速提升工具
  • 深圳建设局网站宝安分中心百度搜索推广官网
  • 网站建设公司小程序开发免费开店的电商平台
  • 城阳网站制作东莞关键词seo优化
  • 网站建设优化服务新闻新闻头条今日最新消息
  • 网站设计psd模板seo关键词优化举例
  • 最近网站不收录河北seo基础入门教程
  • 统一管理网站系统自己开平台怎么弄啊
  • 网站开发文案篮网目前排名
  • 无锡网站推广外包服务福州seo服务
  • 云虚拟主机怎么建设网站郴州seo