|
|
@@ -0,0 +1,299 @@ |
|
|
|
<template> |
|
|
|
<div> |
|
|
|
<!-- <div>123</div>--> |
|
|
|
<!-- <component :is="Comp"></component>--> |
|
|
|
<!-- 直接以函数的形式渲染组件--> |
|
|
|
<!-- <Comp :count="'展示props传参'">--> |
|
|
|
<!-- <div>使用函数组件插槽</div>--> |
|
|
|
<!-- <template #header>--> |
|
|
|
<!-- <div>具名插槽</div>--> |
|
|
|
<!-- </template>--> |
|
|
|
<!-- </Comp>--> |
|
|
|
<!-- <Comp2>--> |
|
|
|
<!-- <div>使用center组件的插槽</div>--> |
|
|
|
<!-- </Comp2>--> |
|
|
|
<!-- <center @onFoo="(val)=>{console.log(val)}"></center>--> |
|
|
|
<Comp3></Comp3> |
|
|
|
<!-- <component :is="Comp3"></component>--> |
|
|
|
<div> |
|
|
|
动态渲染组件 |
|
|
|
<component |
|
|
|
:is="dynamicComponent" |
|
|
|
v-bind="jsonConfig.template.props" |
|
|
|
></component> |
|
|
|
</div> |
|
|
|
</div> |
|
|
|
</template> |
|
|
|
|
|
|
|
<script setup lang="ts"> |
|
|
|
import { compile } from '@vue/compiler-dom' |
|
|
|
import { defineComponent, resolveComponent, h, ref, type FunctionalComponent, onMounted } from 'vue' |
|
|
|
import * as Vue from 'vue' |
|
|
|
import Center from './Center.vue' |
|
|
|
import PageOrDialog from './PageOrDialog.vue' |
|
|
|
import Layout from './Layout.vue' |
|
|
|
import Header from './Header.vue' |
|
|
|
import Panel from './Panel.vue' |
|
|
|
import Footer from './Footer.vue' |
|
|
|
|
|
|
|
// 模拟 JSON 数据 |
|
|
|
const jsonConfig = { |
|
|
|
template: { |
|
|
|
type: 'custom', |
|
|
|
name: 'CustomComponent', |
|
|
|
code: `<div> |
|
|
|
<el-button>这是一个json传入的按钮</el-button> |
|
|
|
<h1>{{ title }}</h1> |
|
|
|
<p>{{ description }}</p> |
|
|
|
</div>`, |
|
|
|
props: { |
|
|
|
title: '动态标题', |
|
|
|
description: '这是通过 JSON 动态渲染的 Vue 代码' |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 编译 JSON 中的 Vue 模板为渲染函数 |
|
|
|
const createDynamicComponent = (template: string) => { |
|
|
|
const { code } = compile(template, { mode: 'function' }) |
|
|
|
// eslint-disable-next-line no-new-func |
|
|
|
const renderFunction = new Function('Vue', code)(Vue) |
|
|
|
|
|
|
|
// 返回一个动态组件 |
|
|
|
return defineComponent({ |
|
|
|
props: ['title', 'description'], |
|
|
|
render: renderFunction |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
// 解析 JSON 并生成动态组件 |
|
|
|
const dynamicComponent = ref() |
|
|
|
if (jsonConfig.template?.code) { |
|
|
|
dynamicComponent.value = createDynamicComponent(jsonConfig.template.code) |
|
|
|
} |
|
|
|
|
|
|
|
// h函数基础使用 |
|
|
|
// const comp = h('div', { style: 'color: red' }, 'Hello World') |
|
|
|
const components = { Layout, PageOrDialog, Header, Panel, Center, Footer } |
|
|
|
// 使用响应式变量 和 使用 子节点的嵌套,事件,属性,插槽的使用 |
|
|
|
const msg = ref('使用变量') |
|
|
|
// 注意这里是一个函数 FunctionalComponent:方法组件类型声明 |
|
|
|
// 这里是声明了一个函数组件,然后被使用 |
|
|
|
const Comp = ((props, { slots }) => { |
|
|
|
return h('div', { style: 'color: red', onClick () { msg.value = '点击了' } }, |
|
|
|
[ |
|
|
|
h('span', {}, msg.value), |
|
|
|
h('h1', {}, props.count), |
|
|
|
h('h1', {}, [slots?.header?.(), slots?.default?.()]) |
|
|
|
]) |
|
|
|
}) as FunctionalComponent<{ count:string }> |
|
|
|
// 这里是声明了一个函数组件,但是是使用的已经定义好的组件 |
|
|
|
const Comp2 = ((props, { slots }) => { |
|
|
|
return h(Center, { |
|
|
|
msg: '我是参数', |
|
|
|
// 事件传参 |
|
|
|
onFoo (val:any) { |
|
|
|
console.log(123123) |
|
|
|
console.log('点击了' + val) |
|
|
|
} |
|
|
|
}, { |
|
|
|
default: slots.default, |
|
|
|
// 这里在footer插槽里面又嵌套了Center组件继续使用插槽,递归组件 |
|
|
|
footer: () => h(Center, null, { |
|
|
|
default: () => h('div', {}, '我是footer插槽'), |
|
|
|
footer: ({ val }) => h('div', {}, val + '使用插槽传参数') |
|
|
|
}) |
|
|
|
}) |
|
|
|
}) as FunctionalComponent |
|
|
|
// 递归解析模板的函数 |
|
|
|
const parseTemplate = (template: any, model: Record<string, any>) => { |
|
|
|
if (!template) return null |
|
|
|
|
|
|
|
const componentName = |
|
|
|
template.type in components |
|
|
|
? components[template.type] |
|
|
|
: resolveComponent(template.type || 'div') |
|
|
|
|
|
|
|
// 查找 model 数据并传递给组件 |
|
|
|
const modelData = model[template.name] || {} |
|
|
|
console.log(modelData, 'modelData') |
|
|
|
console.log(template.type, 'modelData') |
|
|
|
const children = (template.children || []).map((child: any) => |
|
|
|
parseTemplate(child, model) |
|
|
|
) |
|
|
|
|
|
|
|
return h( |
|
|
|
componentName, |
|
|
|
{ |
|
|
|
style: { margin: '20px', padding: '20px' }, |
|
|
|
...template.props, // 组件自带的 props |
|
|
|
data: modelData // 绑定的 model 数据 |
|
|
|
}, |
|
|
|
() => children |
|
|
|
) |
|
|
|
} |
|
|
|
|
|
|
|
// const PageRenderer = defineComponent({ |
|
|
|
// props: ['config'], |
|
|
|
// setup (props) { |
|
|
|
// return () => parseTemplate(template) |
|
|
|
// } |
|
|
|
// }) |
|
|
|
// function bindModel (modelConfig, data) { |
|
|
|
// const model = {} |
|
|
|
// for (const [key, value] of Object.entries(modelConfig)) { |
|
|
|
// model[key] = value.fields.map((field) => ({ |
|
|
|
// ...field, |
|
|
|
// value: data[field.field.split('.')[0]][field.field.split('.')[1]] |
|
|
|
// })) |
|
|
|
// } |
|
|
|
// return model |
|
|
|
// } |
|
|
|
const Comp3 = () => parseTemplate(json.form[0].template, json.form[0].model) |
|
|
|
onMounted(() => { |
|
|
|
// console.log(Comp2) |
|
|
|
// console.log(Comp3()) |
|
|
|
// console.log(parseTemplate(json.form[0].template)) |
|
|
|
}) |
|
|
|
const json = { |
|
|
|
form: [ |
|
|
|
{ |
|
|
|
page: { |
|
|
|
name: '', |
|
|
|
type: 'page/dialog', |
|
|
|
title: '中文标题', |
|
|
|
template: '模板文件,为空则读自定义模板' |
|
|
|
}, |
|
|
|
template: { |
|
|
|
type: 'Layout', |
|
|
|
name: 'l1', |
|
|
|
children: [ |
|
|
|
{ |
|
|
|
type: 'Header', |
|
|
|
name: 'h1', |
|
|
|
props: { |
|
|
|
title: '标题', |
|
|
|
subTitle: '副标题', |
|
|
|
icon: 'icon' |
|
|
|
|
|
|
|
} |
|
|
|
}, |
|
|
|
{ |
|
|
|
type: 'Center', |
|
|
|
name: 'c1', |
|
|
|
children: [ |
|
|
|
{ |
|
|
|
type: 'Panel', |
|
|
|
name: 'p1' |
|
|
|
} |
|
|
|
|
|
|
|
] |
|
|
|
|
|
|
|
}, |
|
|
|
{ |
|
|
|
type: 'Footer', |
|
|
|
name: 'Footer', |
|
|
|
props: { |
|
|
|
title: '标题', |
|
|
|
subTitle: '副标题', |
|
|
|
icon: 'icon' |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
] |
|
|
|
}, |
|
|
|
model: { |
|
|
|
|
|
|
|
p1: { |
|
|
|
vueComponent: { |
|
|
|
vueTemplate: ` |
|
|
|
<div style="background-color: #42b883;height: 180px;line-height: 30px;color: #fff;width: 400px;border-radius: 4px;padding:10px;margin: 0 auto"> |
|
|
|
<el-button type="primary" @click="log123">点击事件测试</el-button> |
|
|
|
|
|
|
|
<h1>{{ title }}</h1> |
|
|
|
<p >{{ description }}</p> |
|
|
|
<p>点击次数:{{ clickCount }}</p> |
|
|
|
<p>计算属性{{capitalizedTitle}}</p> |
|
|
|
|
|
|
|
</div>`, |
|
|
|
props: { |
|
|
|
title: '动态标题', |
|
|
|
description: '这是通过 JSON 动态渲染的 Vue 代码块' |
|
|
|
}, |
|
|
|
methods: { |
|
|
|
log123 () { |
|
|
|
console.log('按钮点击事件触发!') |
|
|
|
this.clickCount++ |
|
|
|
} |
|
|
|
}, |
|
|
|
data: { |
|
|
|
clickCount: 0 |
|
|
|
}, |
|
|
|
computed: { |
|
|
|
capitalizedTitle () { |
|
|
|
return this.clickCount + '00' |
|
|
|
} |
|
|
|
}, |
|
|
|
watch: { |
|
|
|
clickCount (newValue) { |
|
|
|
console.log('点击次数发生变化:', newValue) |
|
|
|
} |
|
|
|
} |
|
|
|
}, |
|
|
|
|
|
|
|
dataset: [ |
|
|
|
{ |
|
|
|
d1: { f1: '我是数据集数据' } |
|
|
|
}, |
|
|
|
{ |
|
|
|
d2: { f2: '我是f2' } |
|
|
|
} |
|
|
|
], |
|
|
|
fields: [ |
|
|
|
{ |
|
|
|
name: 'f1', |
|
|
|
field: 'd1.f1', |
|
|
|
type: 'combo', |
|
|
|
readonly: true |
|
|
|
}, |
|
|
|
{ |
|
|
|
name: 'f1', |
|
|
|
field: 'd1.f1', |
|
|
|
type: 'vue', |
|
|
|
content: { string: '自定义vue放这里' } |
|
|
|
} |
|
|
|
] |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
] |
|
|
|
} |
|
|
|
|
|
|
|
const template = { |
|
|
|
type: 'div', |
|
|
|
props: { class: 'container' }, |
|
|
|
children: [ |
|
|
|
{ |
|
|
|
type: 'span', |
|
|
|
props: { class: 'text' }, |
|
|
|
children: ['Hello'] |
|
|
|
}, |
|
|
|
{ |
|
|
|
type: 'button', |
|
|
|
props: { onClick: () => alert('Clicked!') }, |
|
|
|
children: ['Click me'] |
|
|
|
} |
|
|
|
] |
|
|
|
} |
|
|
|
|
|
|
|
// const vNode = parseTemplate(template) |
|
|
|
// console.log(PageRenderer) |
|
|
|
// console.log(vNode) |
|
|
|
|
|
|
|
</script> |
|
|
|
|
|
|
|
<style scoped lang="less"> |
|
|
|
|
|
|
|
</style> |