Skip to content

组件标签使用v-slotdefault指定插槽属性 ​

本节目标:理解“组件标签使用v-slotdefault指定插槽属性”的核心思路,并能把它用于实际开发或面试表达。 ==当组件内只有一个默认插槽时,在给这个默认插槽传递内容时,====组件的标签就可以被当作插槽的==template==模板来使用。这样我们就可以把== v-slot ==直接用在组件上标签上:==

<current-user v-slot:default="slotProps">  {{ slotProps.user.firstName }}</current-user>

==更简单的做法是使用不带参数的== v-slot ==对应默认插槽:==

<current-user v-slot="slotProps">  {{ slotProps.user.firstName }}</current-user>

注意:==默认插槽的缩写语法====不能====和具名插槽混用,因为它会导致作用域不明确:== \<!-- ==无效,会导致警告==

 --><current-user v-slot="slotProps">  {{ slotProps.user.firstName }}  <template v-slot:other="otherSlotProps">    slotProps is NOT available here  </template></current-user>

所以:==只要出现多个插槽,请始终为==所有的==插槽使用完整的基于== \<template\> ==的语法:==

<current-user>  <template v-slot:default="slotProps">    {{ slotProps.user.firstName }}  </template>
<template v-slot:other="otherSlotProps">    ...  </template></current-user>

本站总访问 — 次