【发布时间】:2021-04-23 05:35:32
【问题描述】:
感谢 Vue.js Scoped Slots,我能够遍历子组件中的所有可用插槽。 现在,我尝试做的是仅在模板中的特定位置呈现以某个字符串开头的插槽。
不幸的是,它不起作用。我可能忽略了一些东西。
Parent.vue:
<Child>
<template #table--item.person_id="{ value }">
<div class="text-caption">{{ value }}</div>
</template>
</Child>
Child.vue:
<template>
<v-data-table>
<template v-for="(_, slotName) of tableSlots" v-slot:[slotName]="scope">
<slot :name="slotName" v-bind="scope"></slot>
</template>
</v-data-table>
</template>
<script>
export default {
computed: {
tableSlots() {
const prefix = 'table--';
const raw = this.$scopedSlots;
const filtered = Object.keys(raw)
.filter(key => key.startsWith(prefix))
.reduce((obj, key) => ({
...obj,
[key.replace(prefix, "")]: raw[key]
}), {});
return filtered;
}
}
}
</script>
【问题讨论】:
标签: javascript vue.js vuejs2 vue-component vuetify.js