Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
T
test-com
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zhuxichen
test-com
Commits
3d5fd1d1
Commit
3d5fd1d1
authored
Nov 04, 2024
by
zhuxichen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: fix .d.ts
parent
40978329
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
76 additions
and
3 deletions
+76
-3
env.d.ts
env.d.ts
+16
-0
DynamicComponentLoader.vue
src/components/DynamicComponentLoader.vue
+54
-0
TheWelcome.vue
src/components/TheWelcome.vue
+6
-3
No files found.
env.d.ts
View file @
3d5fd1d1
/// <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
{}
src/components/DynamicComponentLoader.vue
0 → 100644
View file @
3d5fd1d1
<
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
>
src/components/TheWelcome.vue
View file @
3d5fd1d1
<
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
>
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment