add:添加插槽

This commit is contained in:
lhj
2024-09-23 23:16:50 +08:00
parent 2dccc36d32
commit 67aef1df33
3 changed files with 61 additions and 24 deletions

View File

@ -5,6 +5,11 @@
<template v-for="child in componentChildren" :key="child.id">
<DynamicComponent :component-data="child" />
</template>
<template v-for="(slot,key, index) in componentSlots" :key="index" v-slot:[key]>
<DynamicComponent :component-data="slot" />
</template>
</component>
</template>
@ -23,9 +28,11 @@ onMounted(() => {
const componentType = computed(() => componentMapping[props.componentData?.type] || 'div');
const componentProps = computed(() => props.componentData?.props || {});
const componentChildren = computed(() => props.componentData?.children || []);
const componentText = computed(() => props.componentData?.text ||'');
const componentText = computed(() => props.componentData?.text || '');
const componentClass = computed(() => props.componentData?.class || []);
const componentStyle = computed(() => props.componentData?.style || []);
const componentSlots = computed(() => props.componentData?.slots || []);
</script>

View File

@ -1,18 +1,29 @@
<!-- DynamicSlotsComponent.vue -->
<template>
<div class="my-component">
<slot name="header"></slot>
<slot name="extra"></slot>
<div>
<!-- 动态插槽 -->
<div>
<slot name="header"></slot>
</div>
<div>
<slot name="main"></slot>
</div>
<div>
<slot name="footer"></slot>
</div>
</div>
</template>
<script>
export default {
name: 'MyComponent',
};
</script>
<script setup>
import { defineProps } from 'vue';
<style scoped>
.my-component {
/* 添加样式 */
}
</style>
// 接收一个动态插槽名作为 prop
const props = defineProps({
dynamicSlotName: String
});
</script>

View File

@ -1,5 +1,9 @@
<template>
<div>
<div style="display: flex;flex-direction: row;">
<div ref="el2"
@ -24,14 +28,20 @@
{{ component.id }}
</DynamicComponent>
</div>
<TestComponent>
<template #header>
<div>
header
</div>
<div>
<TestComponent >
<!-- 默认插槽内容 -->
<template #default>
Default slot content.
</template>
<!-- 动态插槽内容 -->
<template v-for="(slot, index) in dynamicSlotContents" :key="index" v-slot:[slot.slotName]>
{{ slot.content }}
</template>
</TestComponent>
</div>
</div>
</template>
@ -41,16 +51,25 @@ import { onMounted, ref } from 'vue';
import NestedFunction from './NestedFunction.vue';
import DynamicComponent from './DynamicComponent.vue';
import { useDraggable } from 'vue-draggable-plus'
import { useDraggable } from 'vue-draggable-plus';
import { uuid } from 'lsp-uuid';
import { IComponent } from '../type/IComponent.ts';
import { componentScheme } from '../schemes/scheme.ts';
import TestComponent from './TestComponent.vue';
const dynamicSlotContents = ref([
{ slotName: 'header', content: 'Header content' },
{ slotName: 'footer', content: 'Footer content' },
{ slotName: 'main', content: 'Main content' }
]);
const componentsList = ref<any[]>([]);
const list = ref<IComponent[]>([])
const el2 = ref()
const list = ref<IComponent[]>([]);
const el2 = ref();
onMounted(() => {
@ -69,7 +88,7 @@ useDraggable(el2, componentsList, {
onClone() {
console.log('clone')
},
clone(element: Record<'id' | 'name' | 'type' | 'children' | 'props' | 'text' | 'class' | 'style', any>) {
clone(element: Record<'id' | 'name' | 'type' | 'children' | 'props' | 'text' | 'class' | 'style'|'slots', any>) {
return {
id: `${element.id}-${uuid()}`,
name: element.name,
@ -81,7 +100,7 @@ useDraggable(el2, componentsList, {
children: [],
style: element.style,
visible: "",
slots: {},
slots: element.slots,
disable: "",
events: {},
loop: {},