Commit 3d5fd1d1 by zhuxichen

fix: fix .d.ts

parent 40978329
/// <reference types="vite/client" />
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { ComponentCustomProperties } from 'vue'
// 定义 $globalConfig 的接口
interface GlobalConfig {
componentKV: Record<string, string>
[v: string]: string | object
}
declare module '@vue/runtime-core' {
interface ComponentCustomProperties {
$globalConfig: GlobalConfig
}
}
export {}
<template>
<component
:is="DynamicComponent"
v-bind="props"
v-on="$attrs"
v-if="DynamicComponent"
/>
</template>
<script setup lang="ts">
import {
ref,
onMounted,
defineAsyncComponent,
defineProps,
getCurrentInstance,
} from 'vue'
const props = defineProps<{
componentKey: string // 组件键
}>()
const instance = getCurrentInstance()
const globalConfig = ref<Record<string, string | object> | undefined>(undefined)
const DynamicComponent = ref(null)
// 动态导入组件
const loadComponent = async (componentPath: string) => {
DynamicComponent.value = defineAsyncComponent(
() => import(/* @vite-ignore */ `${componentPath}`),
)
}
// 获取全局配置和动态组件路径
const fetchGlobalConfigAndComponentPath = async () => {
globalConfig.value =
instance?.appContext.config.globalProperties.$globalConfig
const componentKV = (globalConfig.value?.componentKV || {}) as Record<
string,
string
>
// 可以是一个await 来接受componentPath
const componentPath = componentKV?.[props.componentKey]
if (componentPath) {
loadComponent(componentPath)
} else {
console.warn(`Component path not found for key: ${props.componentKey}`)
}
}
onMounted(() => {
fetchGlobalConfigAndComponentPath()
})
</script>
<script setup lang="ts">
import { ref, onMounted, getCurrentInstance, defineAsyncComponent } from 'vue'
// import TestCom1 from '@/components/testComponent/TestCom1.vue'
const instance = getCurrentInstance()
const DynamicComponent = ref(null)
......@@ -33,6 +32,10 @@ onMounted(() => {
<template>
<div>{{ $globalConfig?.name }}</div>
<!-- <TestCom1 /> -->
<component :is="DynamicComponent" v-if="DynamicComponent" />
<component
:is="DynamicComponent"
v-if="DynamicComponent"
v-bind="props"
v-on="$attrs"
/>
</template>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment