Commit cd49756c by zhuxichen

demo 支持从远程获取组件,检测mode为remote时减少getConfig消耗

parent 3d5fd1d1
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
import { ComponentCustomProperties } from 'vue' import { ComponentCustomProperties } from 'vue'
// 定义 $globalConfig 的接口 // 定义 $globalConfig 的接口
interface GlobalConfig { export interface GlobalConfig {
componentKV: Record<string, string> componentKV: Record<string, string>
[v: string]: string | object [v: string]: string | object
} }
......
...@@ -8,22 +8,23 @@ ...@@ -8,22 +8,23 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { import { useConfig } from '@/config/useConfig'
ref, import { ref, onMounted, defineAsyncComponent, defineProps } from 'vue'
onMounted,
defineAsyncComponent, interface Props {
defineProps,
getCurrentInstance,
} from 'vue'
const props = defineProps<{
componentKey: string // 组件键 componentKey: string // 组件键
}>() // local请求本地的key remote请求接口的key
componentType: 'local' | 'remote'
}
const props = withDefaults(defineProps<Props>(), {
componentType: 'local',
})
const instance = getCurrentInstance()
const globalConfig = ref<Record<string, string | object> | undefined>(undefined)
const DynamicComponent = ref(null) const DynamicComponent = ref(null)
const { fetchGlobalConfig } = useConfig()
// 动态导入组件 // 动态导入组件
const loadComponent = async (componentPath: string) => { const loadComponent = async (componentPath: string) => {
DynamicComponent.value = defineAsyncComponent( DynamicComponent.value = defineAsyncComponent(
...@@ -31,20 +32,25 @@ const loadComponent = async (componentPath: string) => { ...@@ -31,20 +32,25 @@ const loadComponent = async (componentPath: string) => {
) )
} }
// 获取全局配置和动态组件路径 const fetchRmoteComponentPath = async () => {
return Promise.resolve('/src/components/testComponent/TestCom1.vue')
}
// 区分组件类型,以获取组件路径【全局 / 远程】
const fetchGlobalConfigAndComponentPath = async () => { const fetchGlobalConfigAndComponentPath = async () => {
globalConfig.value = let componentPath: string | undefined
instance?.appContext.config.globalProperties.$globalConfig if (props.componentType === 'local') {
const componentKV = (globalConfig.value?.componentKV || {}) as Record< const res = await fetchGlobalConfig()
string, componentPath = res?.componentKV?.[props.componentKey]
string } else {
> componentPath = await fetchRmoteComponentPath()
// 可以是一个await 来接受componentPath }
const componentPath = componentKV?.[props.componentKey]
if (componentPath) { if (componentPath) {
loadComponent(componentPath) loadComponent(componentPath)
} else { } else {
console.warn(`Component path not found for key: ${props.componentKey}`) console.warn(
`Component path with ${props.componentType} not found for key: ${props.componentKey}`,
)
} }
} }
......
import type { GlobalConfig } from 'env'
import { ref, getCurrentInstance } from 'vue'
export const useConfig = () => {
const instance = getCurrentInstance()
const globalConfig = ref<GlobalConfig>()
const fetchGlobalConfig = () => {
if (instance) {
globalConfig.value =
instance.appContext.config.globalProperties.$globalConfig
}
return Promise.resolve(globalConfig.value)
}
return { globalConfig, fetchGlobalConfig }
}
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