在 Vue 3 中,通过 slot 具名参数或 v-slot 范围插槽可以获取插槽数据。使用 slot 具名参数时,slot 数据通过 prop 传递;使用 v-slot 范围插槽时,slot 数据作为参数传递。
Vue 3 如何获取插槽中的数据
在 Vue 3 中,获取插槽中数据的常用方法是使用 slot 具名参数或 v-slot 范围插槽。
使用 slot 具名参数
<template v-slot:header> <h1>插槽内容</h1> </template>
const App = { template: ` <component :header="slotData"> <template v-slot:header> <h1>默认插槽内容</h1> </template> </component> `, data() { return { slotData: '插槽数据' } } }
在父组件中,使用 :header 属性将插槽数据传递给子组件。子组件可以像访问其他 prop 一样访问插槽数据(this.header)。
立即学习“前端免费学习笔记(深入)”;
使用 v-slot 范围插槽
<component> <template v-slot:header> <h1>插槽内容:{{ slotContent }}</h1> </template> </component>
const App = { template: ` <component> <template v-slot:header="slotData"> <h1>默认插槽内容:{{ slotData }}</h1> </template> </component> `, data() { return { slotData: '插槽数据' } } }
在父组件中,使用 v-slot 和一个范围变量(slotData)来将插槽数据传递给子组件。子组件可以访问插槽数据(slotData)。
以上就是vue3怎么获取插槽中的数据的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号