반응형
Vue.js - 필터링된 scopedSlot에서 슬롯을 동적으로 만듭니다.
Vue.js 스코프드 슬롯 덕분에 Child 컴포넌트 내의 사용 가능한 모든 슬롯을 루프할 수 있습니다.템플릿의 특정 위치에 특정 문자열로 시작하는 슬롯만 렌더링하려고 합니다.
불행히도, 그것은 작동하지 않는다.제가 뭔가를 간과하고 있는 것 같아요.
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>
https://codesandbox.io/s/keen-bose-yi8x0
부모가 다음 이름의 슬롯에 접속하려고 합니다.table--item.glutenfree그 때문에, 그 이름의 스코프 슬롯이 작성됩니다.하지만 당신이 그 대상을 정하기 위해 필터링을 할 때v-data-tableslot, 이 필터링된 이름을 자녀 슬롯에도 사용합니다.
key.replace(prefix, "")
부모가 접두사가 그대로인 이름을 대상으로 하고 있기 때문에 부모가 자식 슬롯에 액세스할 수 없습니다.
자 컴포넌트의 슬롯을 변경합니다.
<slot :name="`table--${slotName}`" v-bind="scope"></slot>
언급URL : https://stackoverflow.com/questions/65782691/vue-js-dynamically-create-slots-from-filtered-scopedslots
반응형
'source' 카테고리의 다른 글
| PEP8에 준거하여 E501을 방지하는 매우 긴 문자열 작성 방법 (0) | 2022.09.12 |
|---|---|
| String을 사용하여 JSON 개체를 만드는 방법 (0) | 2022.09.12 |
| Seaborn 막대플롯의 축 레이블 (0) | 2022.09.12 |
| 특정 인덱스에 문자열 삽입 (0) | 2022.09.11 |
| <<>의 용도Drupal과 tcpdf를 사용하여 node to PDF를 구현하고 있습니다. 그런 경우에는 이 태그를 사용해야 합니다. 사용하지 않으면 오류가 발생합니다. 의 목적을 정확히 알 수 없다.이거 콘셉트 .. (0) | 2022.09.11 |