当前位置: 首页 > 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.yayakq.cn/news/231797/

相关文章:

  • 南宁建设网站推广是什么意思
  • 不用下载劰网站的片你懂的村级门户网站建设
  • 网站的搭建流程乐清网站制作公司有哪些
  • 大庆市网站建设公司建设网站合同文档
  • 大连网站推广招聘html制作企业宣传网站
  • 汽车网站建设开题报告郑州企业
  • 邢台网站改版定制网页界面设计的界面结构组成
  • 网站备案增加域名解析wordpress 软件价格
  • 设计最简单的企业网站地推团队如何收费
  • 网站建设如何定价一起做网店潮汕站
  • 建站公司咨询网站建设用哪的图片不侵权
  • 厦门市建设局查询保障摇号网站首页平面广告设计趋势
  • 铜仁 网站开发百度一下你就知道搜索
  • 以网站建设为开题报告长沙做网站的公司哪家最好
  • 济南网络优化网站新加坡网站开发公司
  • 企业网站销售营销型企业网站建设与推广
  • 网站建设的规模与类别一个工厂做网站有什么好处
  • js源码下载从哪个网站能下载如何建立网站视频教程
  • 昆明做网站找启搜网络无锡网站开发平台
  • 网站前端制作费用深圳做网站哪个好
  • 青海省高速公路建设管理局网站网站统计数据怎么做c成apa格式
  • 企业做网站需要那些条件张家界市建设网站
  • 哈尔滨网站建设推广服务软件开发公司照片
  • 秦皇岛网站团队六安短视频优化费用
  • 网站开发与移动互联贵阳网站seo公司
  • 长沙市网站推广多少钱怎么去跟客户谈网站建设
  • 上海 网站建设 500强wordpress腾讯地图插件
  • 做网站用到的单词怎样做类似淘宝网的网站
  • 电子商务专业网站建设知名网站建设企业多少钱
  • 广东网站备案需要多久如何开发app应用