为component添加id,并增加点击获取组件id的功能
This commit is contained in:
@ -1,19 +1,16 @@
|
|||||||
<!-- DynamicComponent.vue -->
|
|
||||||
<template>
|
<template>
|
||||||
<component :is="componentType" v-bind="componentProps" :class="componentClass" :style="componentStyle">
|
<div :id="componentId" @click="handleClick">
|
||||||
{{ componentText }}
|
<component :is="componentType" v-bind="componentProps" :class="componentClass" :style="componentStyle">
|
||||||
<template v-for="child in componentChildren" :key="child.id">
|
{{ componentText }}
|
||||||
<DynamicComponent :component-data="child" />
|
<template v-for="child in componentChildren" :key="child.id">
|
||||||
</template>
|
<DynamicComponent :component-data="child" />
|
||||||
|
</template>
|
||||||
<template v-for="(slot,key, index) in componentSlots" :key="index" v-slot:[key]>
|
<template v-for="(slot, key, index) in componentSlots" :key="index" v-slot:[key]>
|
||||||
<DynamicComponent :component-data="slot" />
|
<DynamicComponent :component-data="slot" />
|
||||||
</template>
|
</template>
|
||||||
|
</component>
|
||||||
</component>
|
</div>
|
||||||
|
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { defineProps, computed, onMounted } from 'vue';
|
import { defineProps, computed, onMounted } from 'vue';
|
||||||
import { componentMapping } from './componentMapping';
|
import { componentMapping } from './componentMapping';
|
||||||
@ -25,6 +22,8 @@ const props = defineProps({
|
|||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
console.log(props.componentData)
|
console.log(props.componentData)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const componentId = computed(() => props.componentData?.id || '');
|
||||||
const componentType = computed(() => componentMapping[props.componentData?.type] || 'div');
|
const componentType = computed(() => componentMapping[props.componentData?.type] || 'div');
|
||||||
const componentProps = computed(() => props.componentData?.props || {});
|
const componentProps = computed(() => props.componentData?.props || {});
|
||||||
const componentChildren = computed(() => props.componentData?.children || []);
|
const componentChildren = computed(() => props.componentData?.children || []);
|
||||||
@ -33,6 +32,8 @@ const componentClass = computed(() => props.componentData?.class || []);
|
|||||||
const componentStyle = computed(() => props.componentData?.style || []);
|
const componentStyle = computed(() => props.componentData?.style || []);
|
||||||
const componentSlots = computed(() => props.componentData?.slots || []);
|
const componentSlots = computed(() => props.componentData?.slots || []);
|
||||||
|
|
||||||
|
const handleClick = () => {
|
||||||
|
console.log(`Div with id ${componentId.value} was clicked.`);
|
||||||
|
// 你可以在这里执行更多的逻辑,例如发出一个事件或调用一个方法
|
||||||
|
};
|
||||||
</script>
|
</script>
|
||||||
@ -78,7 +78,8 @@ useDraggable(el2, componentsList, {
|
|||||||
},
|
},
|
||||||
clone(element: Record<'id' | 'name' | 'type' | 'children' | 'props' | 'text' | 'class' | 'style' | 'slots', any>) {
|
clone(element: Record<'id' | 'name' | 'type' | 'children' | 'props' | 'text' | 'class' | 'style' | 'slots', any>) {
|
||||||
return {
|
return {
|
||||||
id: `${element.id}-${uuid()}`,
|
// id: `${element.id}-${uuid()}`,
|
||||||
|
id: uuid(),
|
||||||
name: element.name,
|
name: element.name,
|
||||||
type: element.type,
|
type: element.type,
|
||||||
props: element.props,
|
props: element.props,
|
||||||
|
|||||||
@ -11,7 +11,7 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { useDraggable} from 'vue-draggable-plus'
|
import { useDraggable} from 'vue-draggable-plus'
|
||||||
import { computed, ref } from 'vue'
|
import { computed, ref } from 'vue'
|
||||||
import { IPageComponent } from '../type/IComponent'
|
import { IPageComponent } from '../type/IPageComponent'
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
modelValue: IPageComponent[]
|
modelValue: IPageComponent[]
|
||||||
@ -37,7 +37,7 @@ useDraggable(el, list, {
|
|||||||
onUpdate() {
|
onUpdate() {
|
||||||
console.log('update list1')
|
console.log('update list1')
|
||||||
},
|
},
|
||||||
onAdd: (e) => {
|
onAdd: () => {
|
||||||
// console.log(e)
|
// console.log(e)
|
||||||
console.log('add list1')
|
console.log('add list1')
|
||||||
},
|
},
|
||||||
|
|||||||
@ -12,7 +12,7 @@
|
|||||||
"visible": "",
|
"visible": "",
|
||||||
"slots": {
|
"slots": {
|
||||||
"extra":{
|
"extra":{
|
||||||
"id": "avatar-9f8289a12910000",
|
"id": "9f8289a12910000",
|
||||||
"name": "avatar",
|
"name": "avatar",
|
||||||
"type": "Avatar",
|
"type": "Avatar",
|
||||||
"props": {},
|
"props": {},
|
||||||
|
|||||||
2
src/type/IComponent.js
Normal file
2
src/type/IComponent.js
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
"use strict";
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
Reference in New Issue
Block a user