Skip to content

Commit

Permalink
自动导入组件
Browse files Browse the repository at this point in the history
  • Loading branch information
陈CC committed Aug 29, 2024
1 parent d8d76d1 commit 115de1b
Show file tree
Hide file tree
Showing 20 changed files with 56 additions and 164 deletions.
10 changes: 0 additions & 10 deletions src/App.vue
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
<script setup>
import { RouterView } from 'vue-router'
// const modules = import.meta.glob('./views/test/**/index.vue');
// console.log(2,modules)
// Object.keys(modules).map(async (path) => {
// const routePath = path
// .replace('../views/', '')
// .replace('/index.vue', '');
// let com = await modules[path]()
// console.log(com)
// console.log(routePath)
// })
</script>

<template>
Expand Down
33 changes: 0 additions & 33 deletions src/components/MyDiv.vue

This file was deleted.

36 changes: 0 additions & 36 deletions src/components/MyFlex.vue

This file was deleted.

33 changes: 0 additions & 33 deletions src/components/MyText.vue

This file was deleted.

5 changes: 5 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ import '@/style/index.scss'
import 'ant-design-vue/dist/reset.css';

const app = createApp(App)

app.component('Empty', Empty);

import { initMaterials } from './material/registerMaterials';
initMaterials(app)

app.use(createPinia())
app.use(router)

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
<script setup>
import { Button } from 'ant-design-vue';
import { defineOptions } from 'vue';
defineOptions({
name: 'MyButton',
})
const props = defineProps({
schema: {
type: Object,
default: () => {}
default: () => {
return {}
}
},
children: {
type: Array,
default: () => []
default: () => {
return []
}
}
})
</script>

<template>
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<script setup>
import Render from '../Render.vue';
import { ref } from 'vue';
import { defineOptions } from 'vue';
defineOptions({
name: 'MyDiv',
})
const props = defineProps({
schema: {
type: Object,
Expand All @@ -10,11 +15,7 @@ const props = defineProps({
default: () => []
}
})
import { onMounted, ref } from 'vue';
const myRef = ref('')
onMounted(() =>{
props.schema.setClientRect( myRef.value.getBoundingClientRect())
})
</script>

<template>
Expand Down
File renamed without changes.
17 changes: 11 additions & 6 deletions src/components/MyFlex/index.vue → src/material/MyFlex/index.vue
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
<script setup>
import Render from '../Render.vue';
import { ref } from 'vue';
import { defineOptions } from 'vue';
defineOptions({
name: 'MyFlex',
})
const props = defineProps({
schema: {
type: Object,
default: () => {}
default: () => {
return {}
}
},
children: {
type: Array,
default: () => []
default: () => {
return []
}
}
})
import { onMounted, ref } from 'vue';
const myRef = ref('')
onMounted(() =>{
props.schema.setClientRect( myRef.value.getBoundingClientRect())
})
</script>

<template>
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<script setup>
import { currentSchema } from '@/stores/globalData.js'
import { defineOptions } from 'vue';
defineOptions({
name: 'MyText',
})
import { _state, _stateKeys } from '@/stores/index.js'
const props = defineProps({
schema: {
type: Object,
Expand Down
5 changes: 2 additions & 3 deletions src/components/Render.vue → src/material/Render.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<script setup>
import { inject } from 'vue';
const props = defineProps({
schema: {
Expand All @@ -11,11 +10,11 @@ const props = defineProps({

<template>
<template v-if="schema.display === 'inline-block'">
<component :is="schema.component" :children="schema.children || []" :schema="schema">
<component :is="schema.componentName" :children="schema.children || []" :schema="schema">
</component>
</template>
<template v-else class="c_wrap">
<component :is="schema.component" :children="schema.children || []" :schema="schema">
<component :is="schema.componentName" :children="schema.children || []" :schema="schema">
<Empty v-if="!schema.children || schema.children.length === 0" />
</component>
</template>
Expand Down
14 changes: 14 additions & 0 deletions src/material/registerMaterials.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

const componentNameRegex = /\/material\/([^/]+)\//
/**
* 遍历material文件夹中的index.vue,注册为全局组件
* @param {Vue实例} app ue实例
*/
export function initMaterials(app){
const modules = import.meta.glob('@/material/**/index.vue',{ eager: true });
for (const path in modules) {
const module = modules[path].default
const componentName = module.name ? module.name : path.match(componentNameRegex)
app.component(componentName, module);
}
}
2 changes: 1 addition & 1 deletion src/stores/globalData.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { nanoid } from 'nanoid'

// 引入物料
const materialMap = new Map()
const modules = import.meta.glob('@/components/**/index.ts',{ eager: true });
const modules = import.meta.glob('@/material/**/index.ts',{ eager: true });
for (const path in modules) {
const module = modules[path].default
if(!materialMap.has(module.group)){
Expand Down
17 changes: 0 additions & 17 deletions src/utils/component.js

This file was deleted.

3 changes: 1 addition & 2 deletions src/utils/material.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// 物料
import { nanoid } from 'nanoid'
import component from './component'
export default class Material{
constructor(type){
this.id = nanoid(10)
this.label = '组件描述' + nanoid(5)
this.display = type === 'Text' || type === 'Button'? 'inline-block' : 'block'
this.component = component.get(type),
this.componentName = type,
this.type = type
this.rect = null,
this.children = [],
Expand Down
2 changes: 1 addition & 1 deletion src/views/layoutContent/index.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script setup>
import TheDrapLocation from './components/TheDrapLocation.vue';
import Render from '@/components/Render.vue';
import Render from '@/material/Render.vue';
import { debounce } from 'lodash-es'
import { onMounted, provide, ref } from 'vue'
import Material from '@/utils/material'
Expand Down
16 changes: 0 additions & 16 deletions src/views/test/index.vue

This file was deleted.

0 comments on commit 115de1b

Please sign in to comment.