使用作用域插槽
步骤 1.在父级中,将旧的不推荐使用的插槽定位语法更新slot="button"
为v-slot
指令:
父.vue
...
<template v-slot:button> ✅
<button>new button</button>
</template>
...
<button slot="button">new button</button> ❌
如何在 Vue 2.6.0+ 中定位插槽
第 2 步。接下来,了解您添加到<slot>
标签的任何属性绑定都将可用于放置在那里的任何插槽内容(这些称为“插槽道具”):
下拉菜单.vue
<slot name="button" :aria-haspopup="true">
第 3 步。Vue 自动创建一个包含第 2 步中的每个绑定的对象,并将其传递给v-slot
表达式,即slotProps
下面的表达式。然后,您可以使用特殊v-bind=""
语法将所有这些绑定传播到按钮上:
Parent.vue更新
<template v-slot:button="slotProps">
<button v-bind="slotProps">new button</button>
</template>
这是一个演示,但遗憾的是,当您使用 kebab-case 属性执行此操作时,它需要使用两个连字符进行破解。我将计划在 Vue GitHub 存储库中为此提交一个问题。
Vue.component('dropdown', {
template: `
<div>
<slot name="button" aria--haspopup="true">
<button aria-haspopup="true">Default Button</button>
</slot>
<div id="name" :aria-expanded="expanded">
<slot />
</div>
</div>`,
data() {
return {
expanded: true
}
}
});
new Vue({
el: "#app",
});
.aria-haspopup {
background: orange;
}
<div id="app">
<dropdown>
<template v-slot:button="slotProps">
<button v-bind="slotProps">new button</button>
</template>
<ul>content</ul>
</dropdown>
</div>
<script src="https://unpkg.com/vue"></script>