javascript - Vue.js - 从过滤的 scopedSlots 动态创建插槽
问题描述
感谢 Vue.js Scoped Slots,我能够遍历子组件中的所有可用插槽。现在,我尝试做的是仅在模板中的特定位置呈现以某个字符串开头的插槽。
不幸的是,它不起作用。我可能忽略了一些东西。
父.vue:
<Child>
<template #table--item.person_id="{ value }">
<div class="text-caption">{{ value }}</div>
</template>
</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>
解决方案
父级尝试访问一个名为的插槽table--item.glutenfree
,因此使用该名称创建了一个作用域插槽。但是,当您过滤以定位相应的v-data-table
插槽时,您还可以将过滤后的名称用于子插槽:
key.replace(prefix, "")
父级无法访问子槽,因为父级定位的名称前缀仍完好无损。
更改子组件中的插槽:
<slot :name="`table--${slotName}`" v-bind="scope"></slot>
推荐阅读
- forms - Flutter:ListView 的滚动视图问题
- python - Django:如何通过删除每个值前面的 Decimal( 来从查询中获取干净的数据?
- javafx - 在 java fx 中找不到 CSS 样式表
- python - 格式化日期时间对象
- python - 您如何在 while 循环中进行“回溯”?
- arrays - BASH:如何调用文本列表文件中的数据之一并将其分配给变量
- c# - 如何在 LinQ 中映射单个对象而不是 IEnumerable
- apache-spark - 如何以编程方式获取有关 PySpark 中执行程序的信息
- amazon-web-services - AWS Polly - 如何减少字符限制?
- amazon-web-services - Amazon AWS Lightsail + Cloudflare - 名称服务器更新?