Vue3中解构插槽prop的基本概念 官网
在Vue3中,解构插槽prop是一种很有用的技术。它允许我们在父组件使用子组件插槽时,对传递过来的prop进行解构。这样可以使代码更简洁,更易读。 例如,子组件传递了一个包含多个属性的对象作为插槽prop,我们可以在父组件中直接解构这些属性。 !-- 子组件 --template #default="{ message }" p{ { message } }/p/templatescript setup// 这里可以定义传递给插槽的propconst slotProps = { message: 'Hello from slot' }/script!-- 父组件 --ChildComponent template #default="{ message }" p{ { message } }/p /template/ChildComponent// 运行结果:页面显示 'Hello from slot' 注意,解构的属性名要与子组件传递的prop属性名一致。